TS built-in conditional types

Pick

Select a series of attributes from Type, these attributes come from Keys (string literals or union types of string literals), and use these attributes to form a new type.

type Test = {
    
    
    name: string;
    age: number;
    salary?: number;
};

//pick
type picked = Pick<Test, "name" | "age">;
// 结果
// type picked = {
    
    
//     name: string;
//     age: number;
// }

ReturnType

Get the return value type of the function

// 比如
type Func = () => User;
type Test = ReturnType<Func>; // Test = User

// 其他例子
type T0 = ReturnType<() => string>; // string
type T1 = ReturnType<<T>() => T>; // unknown
type T2 = ReturnType<<T extends U, U extends number[]>() => T>; // number[]

Guess you like

Origin blog.csdn.net/DeMonliuhui/article/details/129314730