“module/index.d.ts”和“@ types/module/index.d.ts”之间的Typescript冲突
问题描述:
我有第三方模块(“handsontable”),它在模块文件夹中有过时的定义(“ node_modules/handsontable/handsontable.d.ts“),但适当的”index.d.ts“位于/ node_modules/@ types文件夹中。 所以结构如下:“module/index.d.ts”和“@ types/module/index.d.ts”之间的Typescript冲突
/node_modules
/@types
/handsontable
/index.d.ts (LATEST)
/handsontable
/handsontable.d.ts (OUTDATED)
/src/app.ts
我使用ES6模块,我不希望暴露handsontable全球,所以当我写app.ts:
import ht from 'handsontable'
let options: ht.Options
这表明我错误,因为ht.Options
不存在/node_modules/handsontable/handsontable.d.ts
,而它只有在/node_modules/@types/handsontable/index.d.ts
存在反正是有强制打字稿期间从/node_modules/@type/module
加载类型信息3210?
这里是我的tsconfig.json:
{
"exclude": [
"node_modules","build","dist", "typings","types"
],
"include": [
"./src/**/*.ts"
],
"typeAcquisition": {
"enable": true
// "exclude": [ //tried that too
// "handsontable"
// ]
},
"compileOnSave": true,
"compilerOptions": {
"baseUrl": "node_modules",
"paths": {
"src/*":["../src/*"],
"app/*":["../src/*"],
"*":["../src/*", "./node_modules"]
},
"target": "es2016",
//"module": "es6", //es6 is not compatible with webpack.config.ts
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"allowJs": true,
"outDir": "./build",
"experimentalDecorators": true,
"lib": [
"dom",
"es6"
]
}
}
打字稿版本:2.4.2
答
正如评论"node_modules/{module}/{module}.d.ts"
提到总是优先于"node_modules/@types/{module}/index.d.ts"
所以最好解决方法我可以找到迄今为止映射tsconfig.json/compilationOptions /路径:
{
compilationOptions:{
baseUrl:"node_modules",
paths:{
"handsontable":["@types/handsontable"]
}
}
}
什么帮我找到它,是标志TSC --traceResolutions
你怎么在你的'tsconfig'文件有哪些? – Nayish
刚刚更新了信息的问题 –
首先,您需要从排除的文件中删除类型。接下来,您需要添加handontable的类型,最后您需要排除该类型的。 – Nayish