【问题标题】:Typescript custom type definition file does not workTypescript 自定义类型定义文件不起作用
【发布时间】:2020-10-17 01:37:36
【问题描述】:

我已经看过数千个类似问题的答案,但没有一个对我有用。我正在尝试通过创建自己的 .d.ts 文件来使用没有打字稿类型定义的 npm 模块。我总是收到以下错误:Could not find a declaration file for module 'aws-elasticsearch-connector'.

文件结构

tsconfig.json

{
  "include": [
    "src"
  ],
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true
  }
}

aws-elasticsearch-connector.d.ts

declare module 'aws-elasticsearch-connector';

index.ts

import * as AWS from 'aws-sdk'
import { Client } from '@elastic/elasticsearch'
import { createAwsElasticsearchConnector } from 'aws-elasticsearch-connector'

【问题讨论】:

  • 刚刚尝试了这个设置并且没有出现错误“找不到模块'aws-elasticsearch-connector'的声明文件”。如果直接运行编译器时没有出现错误(npx tscyarn tsc),那么我猜 VS Code + TypeScript 集成发生了一些问题,重新启动 VS Code 可能会有所帮助。或者你也可以尝试在 VS Code 中重启 TypeScript 服务器(打开 Command Palette 并搜索“TypeScript: Restart TS server”)

标签: typescript typescript-typings


【解决方案1】:

您可以尝试在您的 tsconfig.json 中设置 include 选项,如下所示:

"include": ["src/**/*"]

包含选项的文档:https://www.typescriptlang.org/tsconfig#include

【讨论】:

    猜你喜欢
    • 2020-08-29
    • 2021-01-12
    • 2011-06-08
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多