vite+vue3+ts中interface,type和Record的使用
- vite+vue3+ts中interface,type和Record的使用
- interface:接口
- type:类型别名
- 扩展
- interface扩展interface(合并)
- interface扩展interface(继承)
- type扩展type(合并)
- interface扩展type
- type扩展interface
- Record
- 参考链接
interface:接口
中文官方文档:https://www.tslang.cn/docs/handbook/interfaces.html
type:类型别名
https://www.tslang.cn/docs/handbook/advanced-types.html
https://www.typescriptlang.org/docs/handbook/2/keyof-types.html
基本类型
复制
组合
| interface People { |
| name: string, |
| weight: number |
| } |
| type Animal = { |
| name: string, |
| } |
| type Cat = People | Animal |
| |
| type Fruit = 'apple' | 'pear' | 'orange'; |
| type Vegetable = 'broccoli' | 'carrot' | 'lettuce'; |
| |
| |
| type HealthyFoods = Fruit | Vegetable; |
| |
| const bf: HealthyFoods = 'broccoli'; |
| console.log(bf); |
| |
| const useDemo = function (food: HealthyFoods) { |
| console.log(`Eating ${food}`); |
| } |
| useDemo('carrot'); |
复制
元组
元组将不同类型的值按照预定义的顺序组合在一起,每个位置都有固定的类型,且位置的顺序在定义时是确定的
| type ResponseCode = [string, number]; |
复制
| interface Dog { |
| name: string; |
| } |
| interface Cat { |
| age: number; |
| } |
| type Animal = [Dog, Cat]; |
| |
| |
| const animal1: Animal = [ |
| { name: "Fido" }, |
| { age: 3 } |
| ]; |
| console.log(animal1[0].name); |
| console.log(animal1[1].age); |
复制
类型捕捉
| const orange = { color: "Orange", vitamin: 1} |
| |
| type Fruit = typeof orange |
| let apple: Fruit; |
| apple = { color: "Red", vitamin: 2 } |
复制
type 会自动捕捉到 color 是 string 类型,vitamin 是 number 类型
| let div = document.createElement("div"); |
| type B = typeof div; |
复制
遍历属性
| type Keys = "firstName" | "lastName"; |
| |
| type Name = { |
| [key in Keys]: string; |
| }; |
| |
| const myName: Name = { |
| firstName: "kelen", |
| lastName: "huang", |
| }; |
复制
扩展
interface 总是可扩展的
interface扩展interface(合并)
| interface Animal { |
| name: string |
| } |
| interface Animal { |
| honey: boolean |
| } |
| |
| const bear: Animal = { |
| name: 'wine', |
| honey: true |
| } |
复制
interface扩展interface(继承)
| interface Animal { |
| name: string |
| } |
| interface Bear extends Animal { |
| honey: boolean |
| } |
| |
| const bear: Bear = { |
| name: 'wine', |
| honey: true |
| } |
| |
复制
type扩展type(合并)
type创建后不能更改,不能通过同一个名称扩展,只能通过&
去扩展
| type Animal { |
| name: string |
| } |
| type Bear & Animal { |
| honey: boolean |
| } |
| |
| const bear:Bear = { |
| name: 'wine', |
| honey: true |
| } |
复制
interface扩展type
| type Animal = { |
| name: string, |
| } |
| |
| interface Bear extends Animal { |
| honey: boolean; |
| } |
复制
type扩展interface
| interface Animal { |
| name: string, |
| } |
| |
| type Bear = Animal & { |
| honey: boolean |
| } |
复制
Record
https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type
Record<key type, value type>
第一个key
值类型,第二个为obj[key]
数据的类型Record<string, unknown>
任意对象
简单使用
| let termMap1 = {} as Record<string, string>; |
| |
| termMap1 = { |
| age:10, |
| num:'11' |
| } |
| console.log('termMap1',termMap1 ); |
复制
复杂对象
| |
| type P = { |
| [key: string]: number; |
| }; |
| let p: P = { ppp: 10, ooo: 10 }; |
| |
| |
| |
| type PP = Record<string, number>; |
| let pp: PP = { |
| ppp: 10, |
| ooo: 10, |
| }; |
| |
| type PP2 = Record<string, string>; |
| let pp2: PP2 = { |
| ppp: "10", |
| ooo: "10", |
| }; |
| |
| |
| interface term { |
| info: number; |
| checked: boolean; |
| } |
| type TermMap = Record<string, term>; |
| |
| let termMap: TermMap = { |
| xxx: { |
| info: 10, |
| checked: true, |
| }, |
| }; |
| |
| |
| interface info_config { |
| name: string; |
| } |
| interface term2 { |
| info: info_config; |
| checked: boolean; |
| } |
| let termMap2 = {} as Record<string, term2>; |
| |
| termMap2 = { |
| xxx: { |
| info: { |
| name: "111", |
| }, |
| checked: true, |
| }, |
| }; |
| |
| |
| interface info_config3 { |
| name: string[]; |
| } |
| interface term3 { |
| info: info_config3; |
| checked: boolean; |
| } |
| let termMap3 = {} as Record<string, term3>; |
| |
| termMap3 = { |
| xxx: { |
| info: { |
| name: ["1", "2"], |
| }, |
| checked: true, |
| }, |
| }; |
复制