我正在使用打字稿,但无法使用其他文件中声明的接口。
我的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
...
}
答案 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,
}
确保此文件中没有export
或import
。