导入index.d.ts会破坏我的类型



tsconfig.json

{
"compilerOptions": {
"baseUrl": "./",
"target": "es6",                          
"module": "commonjs",                    
"declaration": true,                   
"strict": true,                          
"esModuleInterop": true,                  
"forceConsistentCasingInFileNames": true, 
"typeRoots": [
"node_modules/@types",
"@types"
],
"paths": {
"@types/*": ["@types/*"]
},
},
"exclude": ["node_modules"],
"include": ["./*", "@types"]
}

@types/index.d.ts

import { ExecException } from 'child_process';
type error = ExecException | null;
interface Person {
is: boolean;
str: string;
}
type doThingX = (is: boolean) => boolean;

示例.ts

const jon: Person = {
is: 'hello world'
}
const doThing: TdoThing = x => `${x}`;

如果我注释掉导入,那么现有的类型Person和doThing就会在example.ts中找到并工作如果我保留对导入的引用,那么它就会中断,example.ts无法找到doThing的Person的类型。

找不到名称"Person".ts(2304(导出的变量"jon"具有或正在使用私有名称"Person"。ts(4025(

找不到名称"TdoThing".ts(2304(导出的变量"doThing"具有或正在使用私有名称"TdoThing"。

好的,我找到了原因:信用:导入定义文件(*d.ts(中的类

我的解决方案:

指数.d.ts

type error = import('child_process').ExecException | null;
interface Person {
is: boolean;
str: string;
}
type TdoThing = (is: boolean) => boolean;

相关内容

最新更新