如何使打字稿声明文件适用于函数类型

时间:2020-09-25 08:32:08

标签: typescript type-declaration

这是我的代码:

tsconfig.js

{
  "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

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

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

example.ts

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

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

在example.js中:

  • 找到并使用“人”的界面
  • 找不到'doThing'的类型

1 个答案:

答案 0 :(得分:0)

似乎唯一确定的方法是创建一个名称不同的类型,但可以将其用于任何函数。

index.d.ts

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

example.d.ts

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

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