导入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;

example.ts

const jon: Person = {
  is: 'hello world'
}

const doThing: TdoThing = x => `${x}`;

如果我注释掉导入,则将找到现有类型-Person和doThing并在example.ts中工作 如果我保留对导入的引用,则它将中断,并且example.ts无法找到doThing人员的类型。

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

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

解决方法

好的,我找到了原因: 信用:Import class in definition file (*d.ts)

我的解决方法:

index.d.ts

type error = import('child_process').ExecException | null;

interface Person {
  is: boolean;
  str: string;
}

type TdoThing = (is: boolean) => boolean;