打字稿:无法声明接口

时间:2020-10-23 07:37:48

标签: typescript declaration typescript-typings

我正在使用打字稿,但无法使用其他文件中声明的接口。
我的global.d.ts看起来像这样。

declare interface IPropSendEmail {
    from: string,
    to: string,
    subject: string,
    html: string,
}

下面是tsconfig.json

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "outDir": "./build",
        "esModuleInterop": true,
        "declaration": true,
        "strict": true,
        "typeRoots": [
            "types"
        ],
    }
}

目录结构如下

    | -- src
            |-- types
                     |-- global.d.ts
            |-- util
                     |-- common.ts
            |-- ...

common.ts文件中使用它

export async function sendEmail({ from, to, subject, html }: IPropSendEmail) {
     // function code here
     ...
}

1 个答案:

答案 0 :(得分:1)

应该可以。但是,我建议清除配置的奇怪部分:

在您的tsconfig.json中:

  • 删除typeRoots;
  • 添加include
{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "outDir": "build",
        "esModuleInterop": true,
        "declaration": true,
        "strict": true
    },
    "include": ["src"]
}

global.d.ts中,关键字declare无用:

interface IPropSendEmail {
    from: string,
    to: string,
    subject: string,
    html: string,
}

确保此文件中没有exportimport