前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >7 种常用的 TypeScript 类型

7 种常用的 TypeScript 类型

作者头像
yiyun
发布2023-09-20 09:24:38
1950
发布2023-09-20 09:24:38
举报
文章被收录于专栏:yiyun 的专栏yiyun 的专栏

引言 七种非常有用的 TypeScript 类型,包括获取对象类型、函数返回类型、嵌套类型的查看、可选属性和排除属性等。 typeof

代码语言:javascript
复制
const obj = {
    name: "yiyun",
    age: 16
}

type Person = typeof obj

keyof

代码语言:javascript
复制
const obj = {
    name: "yiyun",
    age: 16
}

type Person = keyof typeof obj

ReturnType

代码语言:javascript
复制
const func = () => {
    const val = "string"
    
    return val
}

type Return = ReturnType<typeof func>

添加 async

代码语言:javascript
复制
const func = async () => {
    const val = "string"

    return val
}

type Return = ReturnType<typeof func>

添加 Awaited<T>

代码语言:javascript
复制
const func = async () => {
    const val = "string"

    return val
}

type Return = Awaited<ReturnType<typeof func>>

Prettify<NestedType>

代码语言:javascript
复制
interface MainType {
    name: string,
    age: number
}

type NestedType = MainType & {
    isDeveloper: boolean
}

如图,这么做, 不易看, 只能看到局部

代码语言:javascript
复制
interface MainType {
    name: string,
    age: number
}

type NestedType = MainType & {
    isDeveloper: boolean
}

type Prettify<T> = {
    [K in keyof T]: T[K]
} & {}


type idk = Prettify<NestedType>

现在可以看到全部了, 非常完整易看 Partial<T> 与 Required<T>

代码语言:javascript
复制
interface Todo {
    title: string,
    description: string
}

// Partial<Todo> 的作用 等同于 下方 , 使其所有字段都变成 可选
// interface Todo {
//     title?: string,
//     description?: string
// }

const updateTodo = (todo: Todo, fieldsToUpdate: <Todo>) => {
    return { ...todo, fieldsToUpdate }
}

const initialTodo: Todo = {
    title: "洗碗",
    description: "锅留着下次再洗"
}

const updatedTodo = updateTodo(initialTodo, {})

现在 title 与 description 都变成可选了, 以至于可传空对象 {}

这不是我们所期望的, 因此将 Partial 改为与之相反的 Required

代码语言:javascript
复制
interface Todo {
    title: string,
    description: string
}

// Required<Todo> 的作用 会使得其中所有都成为必需

const updateTodo = (todo: Todo, fieldsToUpdate: Required<Todo>) => {
    return { ...todo, fieldsToUpdate }
}

const initialTodo: Todo = {
    title: "洗碗",
    description: "锅留着下次再洗"
}

const updatedTodo = updateTodo(initialTodo, {})

注意: 最初 可选(?) , 再加上 Required , 那么最终是 必需 的

Omit<T, K extends string | number | symbol> 与 Exclude

代码语言:javascript
复制
interface Todo {
    title: string,
    description: string
}

const todo: Todo = {
    title: "",
    description: ""
}

type Omitted = Omit<Todo, "title">

添加一个 createdAt

代码语言:javascript
复制
interface Todo {
    title: string,
    description: string,
    createdAt: Date
}

// const todo: Todo = {
//     title: "",
//     description: ""
// }

type Omitted = Omit<Todo, "createdAt">

对于复杂情况, Omit 不总是适用

代码语言:javascript
复制
type Shapes =
    | {
        kind: "circle",
        radius: number
    }
    | {
        kind: "square",
        x: number
    }

type Omitted = Omit<Shapes, { kind: "circle" }>

因此我们不要使用它来排除

代码语言:javascript
复制
type Shapes =
    | {
        kind: "circle",
        radius: number
    }
    | {
        kind: "square",
        x: number
    }

type Omitted = Exclude<Shapes, { kind: "circle" }>

Q&A 补充 参考 感谢帮助! 7 Awesome TypeScript Types You Should Know - YouTube

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档