vue2或vue3中使用xx.d.ts文件(没有提供内置的 TypeScript 类型声明)
1,创建一个xx.d.ts文件里面使用declare 声明要申明的数据,例子随便目录下创建个device.d.ts文件如下就可以直接使用里面数据:
declare namespace DeviceTree {interface firstFloor {province: string, // 省份orgId:stringcitys: DeviceTree.secondFloor[]}interface secondFloor {city: string, // 城市orgId:stringvoltages: DeviceTree.thirdFloor[]}
}2,.d.ts 文件明明没有显示引入,为什么就生效了(在 tsconfig.json 的 include 范围内,只要你的 .d.ts 文件路径在 include 的匹配范围内,TS 编译器就会自动加载它:)
TypeScript 在编译一个项目时,首先会加载项目根目录下的 tsconfig.json,它会根据其中的配置项决定:
要包含哪些文件、 要排除哪些文件 、要使用哪些类型库(如 DOM、ESNext)、 要如何解析模块路径(如路径别名)
3,tsconfig.json例子
{"compilerOptions": {"target": "esnext","module": "esnext","strict": true,"jsx": "preserve","importHelpers": true,"moduleResolution": "node","allowJs": true,"skipLibCheck": true,"esModuleInterop": true,"allowSyntheticDefaultImports": true,"experimentalDecorators": true,"sourceMap": true,"noImplicitAny": false,"baseUrl": ".","types": ["webpack-env"],"paths": {"@/*": ["src/*"]},"lib": ["esnext","dom","dom.iterable","scripthost"]},"include": ["src/**/*.ts","src/**/*.d.ts","src/**/*.tsx","src/**/*.vue","tests/**/*.ts","tests/**/*.tsx",],"exclude": ["node_modules"]
}
4,其实简单的数据也可以不用写.d.ts文件,直接代码里面声明即可(declare var $: any;)
