自定义打字稿定义在 Windows 10 上不起作用
Posted
技术标签:
【中文标题】自定义打字稿定义在 Windows 10 上不起作用【英文标题】:custom typescript definitions not working on windows 10 【发布时间】:2021-01-30 23:40:16 【问题描述】:我有一个基于 Vue 2.x CLI 的项目,它使用 typescript 和 vue 类组件运行。这里我想使用一个名为 vueisotope 的包,它没有类型定义文件。为了使它与打字稿一起工作,我更改了我的 tsconfig.json 以替代在“customTypes”文件夹中搜索 .d.ts 文件。我还创建了一个 customTypes/vueisotope/index.d.ts 文件并声明了该模块。在 Ubuntu 上该项目运行良好,但在 Windows 10 上我仍然收到以下错误:
15:24 Could not find a declaration file for module 'vueisotope'. 'node_modules/vueisotope/dist/vue_isotope.min.js' implicitly has an 'any' type.
Try npm install @types/vueisotope if it exists or add a new declaration (.d.ts) file containing declare module 'vueisotope';
我也尝试将该文件作为环境模块引用,但这也不起作用:
/// <reference path = "../customTypes/vueisotope/index.d.ts" />
我在这里做错了什么还是 Windows/TypeScript/IDE 问题?我怎样才能让它在 Windows 机器上工作?
我的 tsconfig:
"compilerOptions":
"target": "esnext",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"typeRoots": [
"./customTypes",
"./node_modules/@types"
],
"types": [
"webpack-env"
],
"paths":
"@/*": [
"src/*"
]
,
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
,
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules",
"customTypes"
]
我的 index.d.ts:
declare module "vueisotope"
【问题讨论】:
【参考方案1】:我只是没有正确理解typeRoots
部分。我还必须在 types
部分中定义 vueisotope。
所以工作的 tsconfig 将是:
"compilerOptions":
"target": "esnext",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"typeRoots": [
"./customTypes",
"./node_modules/@types"
],
"types": [
"vueisotope",
"webpack-env"
],
"paths":
"@/*": [
"src/*"
]
,
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
,
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules",
"customTypes"
]
【讨论】:
以上是关于自定义打字稿定义在 Windows 10 上不起作用的主要内容,如果未能解决你的问题,请参考以下文章
在 ionic2 中添加自定义用户定义的 Cordova 插件,但打字稿不起作用