我尝试只使用一个文件来保存将在整个应用程序中使用的大多数类型定义,我创建了一个名为
@types
和一个
index.d.ts
导出我想要的每个接口/类型的文件。
更新了我的tsconfig.json文件要包含该文件夹:
{
"compilerOptions": {
"outDir": "./built",
"allowJs": true,
"target": "es5",
"jsx": "react",
"allowSyntheticDefaultImports": true,
"lib": [
"es2015"
],
"typeRoots": [
"./@types"
],
},
"include": [
"./client/**/*",
"./@types/**/*"
],
"awesomeTypescriptLoaderOptions": {
"silent": true,
"errorsAsWarnings": true
}
}
即使在代码中(在client/…)我引用index.d.ts上的一个接口时,vscode抛出一个“找不到名称”。
有没有更好的方法来解决这个问题?
@types/index.d.ts中存在接口定义。
export interface Agent {
name: string;
...
}
interface Props extends RouteComponentProps<any> {
agent?: types.Agent;
}