首页 > 解决方案 > TSC 未找到 NPM 库的自定义 .d.ts 文件

问题描述

import audioConcat from 'audioconcat';
Could not find a declaration file for module 'audioconcat'. '/home/owner/projectDir/node_modules/audioconcat/index.js' implicitly has an 'any' type.
  Try `npm install @types/audioconcat` if it exists or add a new declaration (.d.ts) file containing `declare module 'audioconcat';`ts(7016)

这个 NPM 库没有自己的类型,或者绝对类型上的社区包:https ://www.npmjs.com/package/audioconcat

// src/types/audioconcat/index.d.ts

declare module "audioconcat" {
    export default class AudioConcat {
     constructor(audios: Array<string>);

     VERSION: string;

     ffmpeg: Function;

     concat(images: string[], options: any): this;

     on(event: 'start', callback: (command: any) => void): this;
     on(event: 'error', callback: (err: any, stdout: any, stderr: any) => void):  this;
     on(event: 'end', callback: (output: any) => void): this; 
  } 
}

还有我的 tsconfig.json:

{
   "compilerOptions": {
       "target" : "ES5",
       "module": "commonjs",
       "noImplicitAny": true,
       "removeComments": true,
       "sourceMap": true
       , "outDir": "compiled/"
       , "esModuleInterop": true
       , "strict": true
       , "typeRoots": ["./src/types", "node_modules/@types"]
   },
   "include": ["src/**/*"]
   , "exclude": ["node_modules", "compiled", "__tests__", "types"]
}

// On line 1: Cannot find type definition file for 'audioconcat'.ts

我缺少什么细节让 Typescript 在重新运行后不检测这个声明文件tsc

标签: typescripttypescript-declarations

解决方案


推荐阅读