類型聲明文件
兩種文件類型區(qū)別
創(chuàng)建自己的類型聲明文件
為已有js文件提供類型聲明
給js文件提供聲明文件
同目錄下 utils.js文件
let num = 666
let someOne = 'jame'
let post = {
x: 11,
y: 12
}
const add = (x, y) => {
return x + y
}
const changePost = (post) => {
console.log(post)
}
const formatPoint = (point) => {
console.log('當(dāng)前坐標(biāo)是', point)
}
export { num, someOne, post, add, changePost, formatPoint}
同目錄下 utils.d.ts文件
declare let num:number
declare let someOne
interface PostType {
x: number
y: number
}
declare let post:PostType
declare const add:(x: number, y: number) => number
declare const changePost: (post: PostType) => void
declare const formatPoint: (post: PostType) => void
export { num, someOne, post, add, changePost, formatPoint}
用的時候
import { add } from './utils' // 沒有聲明文件的時候 報錯 無法找到模塊“./utils”的聲明文件
add() // utils.d.ts(8, 20): 未提供 "x" 的自變量 add(x: number, y: number): number