类型声明文件

2024-02-15  本文已影响0人  0说
类型声明文件 两种文件类型区别



创建自己的类型声明文件
为已有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('当前坐标是', 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
上一篇 下一篇

猜你喜欢

热点阅读