代码之家  ›  专栏  ›  技术社区  ›  Hayk Safaryan

typescript无法识别d.ts模块

  •  1
  • Hayk Safaryan  · 技术社区  · 7 年前

    尝试将typescript与koa一起使用。

    koa-respond @koa/cors 没有 @types 包,所以我添加了一个文件 ambient-types.d.ts 在我的SRC中包含这些声明

    declare module 'koa-respond'
    declare module '@koa/cors'
    

    但是我还是从TS得到这些错误

    Try `npm install @types/koa__cors` if it exists or add a new declaration (.d.ts) file containing `declare module 'koa__cors';`
    src/server.ts(7,26): error TS7016: Could not find a declaration file for module 'koa-respond'. '/home/hayk/projects/learning/koa/koala/backend/node_modules/koa-respond/lib/koa-respond.js' implicitly has an 'any' type.
      Try `npm install @types/koa-respond` if it exists or add a new declaration (.d.ts) file containing `declare module 'koa-respond';`
    

    这是我的 tsconfig.json

    {
    "compilerOptions": {
        "module": "commonjs",
        "target": "es2017",
        "noImplicitAny": true,
        "outDir": "./dist",
        "sourceMap": true
    },
    "include": [
        "./src/**/*"
    ]
    }
    

    我就这样跑 nodemon --watch 'src/**/*' -e ts,tsx --exec 'ts-node' ./index.ts

    2 回复  |  直到 7 年前
        1
  •  2
  •   Matt McCutchen    7 年前

    根据我的测试,您需要指定 --files 选择 ts-node 强制它加载与 include 设置在您的 tsconfig.json ,否则 ambient-types.d.ts 从不上膛。

    nodemon --watch 'src/**/*' -e ts,tsx --exec 'ts-node --files' ./index.ts
    
        2
  •  0
  •   Daniel Khoroshko    7 年前

    您可能需要指定 typeRoots tsconfig.json中的目录 在这个目录中,我通常创建与要为其编写类型的模块同名的子目录。 每个目录都有一个 index.d.ts 文件,带 declare module ...... { ... } 里面的东西

    / 
    /tsconfig.json
    /typings
    /typings/my-module/index.d.ts
    
    推荐文章