js的原型链 prototype
title: js的原型链 prototype id: af4efcdffdb5e92e8f02c53e57924895 tags:
知识点 date: 2000/01/01 00:00:00 updated: 2024/01/28 21:37:59 isPublic: true --#|[分隔]|#--
TS常用技巧
为window添加字段
// 给Window添加字段
declare interface Window {
flag: string, // 给Window添加这个字段
apple: string | number, // 给Window添加这个字段
}
为无声明文件的第三方包添加声明
// 可以给qs这个无类型的第三方包声明类型,声明成了any
declare module 'qs';
字符串数组生成type
把下面这个字符串数组,每一项的数组,转为 type 罗列出来:
const arr = ['apple', 'car', 'tree']
// 目标,根据上面的现有的数组,得到下面的类型
type Arr = 'apple' | 'car' | 'tree'
进行转换的方法:
// 先把数组进行 as const 转为不可修改的常量
const arr = ['apple', 'car', 'tree'] as const
// 使用方法生成类型
type Arr = typeof arr[number]
// 最终 type Arr = 'apple' | 'car' | 'tree'
把对象的key生成type
let obj = {
apple: 1,
car: 2,
tree: 3,
}
// 目标,根据上面的现有的对象,得到下面的类型
type ObjKey = 'apple' | 'car' | 'tree'
进行转换的方法:
// 直接使用方法生成类型
type ObjKey = keyof typeof obj;
// 最终 type ObjKey = 'apple' | 'car' | 'tree'
Last updated
Was this helpful?