微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决导入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;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。