TS声明
如何写一个TS声明文件
test.ts
export interface Person {
name: string
age: number
}
const a = function (p: Person) {
console.log(p)
}
export default a
tsc test.ts -d
生成test.d.ts文件,就是声明文件
-d的全称是declaration(声明)
如何提供给别人使用 *.d.ts
库:在package.json添加types字段
{
"name": "ts",
"version": "1.0.0",
"description": "",
"main": "main.js",
"type":"main.d.ts",//添加这里
}
社区:上传到DefinitelyTyped
如何给任意JS添加声明
require方式
v.js
function add(a, b) {
return a + b
}
export default add
tsconfig.json
{
"compilerOptions": {
"types": [
"node"
],
"typeRoots": [
"node_modules/@types"
]
}
}
test.ts
{
interface V {
(a: number, b: number): number
}
const add: V = require('./v.js')
add(1, 2)
}
import 方式
test.ts
import add from './v.js'
add(1, 2)
test.d.ts
declare function y(a: number, b: number): number
export default y
如何修改window全局变量
declare global {
interface Window{
server:{
host:string
}
}
}
版权声明:本博客所有文章除特殊声明外,均采用 CC BY-NC 4.0 许可协议。转载请注明出处 Roxas Deng的博客!