TypeScript Omit Pick Partial 的用法

TypeScript About 714 words

Omit

Omit<原类型, 要删除的键>,使用|选择多个键

interface User {
    name: string,
    age: number,
    password: string
}

type UserWithoutPwd = Omit<User, 'password'>;

const userWithoutPwd: UserWithoutPwd = {
    name: 'zhangsan',
    age: 18,
}

Pick

Pick<原类型, 要保留的键>,使用|选择多个键

interface User {
    name: string,
    age: number,
    password: string
}

type UserWithoutPwd2 = Pick<User, 'name' | 'age'>;

const userWithoutPwd: UserWithoutPwd = {
    name: 'lisi',
    age: 20,
}

Partial

Partial<T>,将泛型中指定的类型中的所有字段变为可选。

interface User {
    name: string,
    age: number,
    password: string
}

type PartialUser = Partial<User>;

const partialUser = {
    name: 'wangwu'
}

等价于

{name?: string, age?: number, password?: string}
Views: 35 · Posted: 2025-10-08

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓
Today In History
Browsing Refresh