我正在尝试在 *.d.ts 文件中编写一个非常简单的环境类型。类型(接口)称为“IField”,我试图不从导入中使用它,而是作为环境类型使用它。不知何故,我在其他项目中成功使用了环境类型,但我似乎无法在这里重现它。
Intellisense 识别我的“IField”接口,但 Typescript 编译器不识别;相反,它返回以下错误:
- 错误 TS2304:找不到名称“IField”
X:\...\testglobal\node_modules\ts-node\src\index.ts:744
return new TSError(diagnosticText, diagnosticCodes);
^
TSError: ⨯ Unable to compile TypeScript:
src/index.ts:2:17 - error TS2304: Cannot find name 'IField'.
2 const t = {} as IField;
....
我玩过 tsconfig,到目前为止没有任何效果。除了导入它之外,关于如何解决这个问题的任何建议?智能如何识别我的环境类型,而不是编译器?
包.json
{
"name": "testglobal",
"version": "1.0.0",
"description": "",
"main": "src/index.ts",
"scripts": {
"start": "nodemon src/index.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Fred Jean-Germain",
"license": "ISC",
"dependencies": {
"@types/node": "^16.11.1",
"nodemon": "^2.0.13",
"typescript": "^4.4.4"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
},
"paths": {
"*": ["./*.d.ts"]
},
"exclude": [
"node_modules"
]
}
类型.d.ts
declare interface IField {
name:string
}
src/index.ts
const t = {name:"asdsa"} as IField;
export function Test() {
console.log('test', t.name);
}
Test();