ts
  KRsXEGSB49bk 2023年11月30日 19 0
①. 在TS中,数组、字符串、数组、接口非常常见

②. 如果要定义一个对象的key和value类型,可以用到TS的Record:
    a. Record后面的泛型就是对象键和值的类型

③. Record<K, T>构造具有给定类型T的一组属性K的类型

(1). 之前写法:

const nav: any = {}

const nav: { [key: 'A' | 'B' | 'C'] : number } = {}

(2). 需要一个对象,有ABC三个属性,属性的值必须是数字:

// 示例1
type keys = 'A' | 'B' | 'C'

const result: Record<keys, number> = {
  A: 1,
  B: 2,
  C: 3
}

// 示例2
interface PageInfo {
  title: string;
}

type Page = "home" | "about" | "contact"

const nav: Record<Page, PageInfo> = {
  about: { title: "about" },
  contact: { title: "contact" },
  home: { title: "home" },
}
①. Partial可以快速把某个接口类型中定义的属性变成可选的(Optional)

(1). Partial类型的定义:

// Make all properties in T optional
type Partial<T> = {
    [P in keyof T]?: T[P];
}

// 定义user的接口
interface IUser {
  name: string
  age: number
  department: string
}
// 经过Partial类型转化后得到
type optional = Partial<IUser>
// optional的结果:
type optional = {
    name?: string | undefined;
    age?: number | undefined;
    department?: string | undefined;
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

上一篇: UF_LAYOUT 布局相关 下一篇: UF_LAYER 图层操作
  1. 分享:
最后一次编辑于 2023年11月30日 0

暂无评论

推荐阅读
KRsXEGSB49bk
作者其他文章 更多

2023-12-19

2023-12-11

2023-12-10

2023-12-10

2023-12-08

2023-12-07

2023-12-05

2023-12-04

ts

2023-11-30