我在项目中有JS/TS代码库,其中一些文件看起来像这样:
import Something from '@some-lib/things/something'; // index.jsx file here
const someFunction = () => {
// do something with "Something"
};
在 VS Code 和 tsc 中,我遇到了以下错误:
``` 找不到模块 '@some-lib/things/something' 的声明文件。 '/Users/user/Projects/project/node_modules/@some-lib/things/something/index.jsx' 隐式地具有 'any' 类型。 如果存在,请尝试运行命令
npm install ...
,或者创建一个新的声明文件(.d.ts),其中包含 declare module '@some-lib/things/something';
```我尝试通过创建文件 `src/@types/@some-lib/index.d.ts` 并添加以下内容来增加定义:
declare module '@some-lib/things/something' {
const Something: (props: React.SVGProps<SVGSVGElement> & {
size?: number,
color?: string,
inline?: boolean,
className?: string,
}) => React.ReactElement;
export default Icon;
}
但我遇到了这个错误:
增强时模块名无效。
模块“@some-lib/things/something”在“/Users/user/Projects/project/node_modules/@some-lib/things/something/index.jsx”处解析为未类型化的模块,无法进行增强。
请帮我。我该如何为npm中具有子目录/子模块的JS库声明TypeScript类型?
{ "compilerOptions": { "module": "commonjs", "esModuleInterop": true, "target": "es2017", "lib": ["es2017", "esnext.asynciterable"], "noImplicitAny": true, "moduleResolution": "node", "sourceMap": true, "outDir": "lib", "emitDecoratorMetadata": true, "experimentalDecorators": true, "skipLibCheck": true, "strict": true }, "include": [ "src" ] }
- Mihályi Zoltánd.ts
文件中的信息所替换? - krutoo