【问题标题】:importing into index.d.ts is breaking my types导入 index.d.ts 正在破坏我的类型
【发布时间】:2020-09-25 11:03:24
【问题描述】:

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 无法找到 Person of doThing 的类型。

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

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

【问题讨论】:

    标签: typescript typescript-declarations


    【解决方案1】:

    好的,我找到了原因: 信用: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;
    

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      • 2014-11-07
      • 2020-03-20
      • 2012-03-10
      相关资源
      最近更新 更多