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

问题描述

这是我的代码

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'的类型

解决方法

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

index.d.ts

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

example.d.ts

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

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