Typescript类型体操 - OmitByType

题目

中文

T中选出一组无法分配给U的属性

示例:

type OmitBoolean = OmitByType<
    {
        name: string;
        count: number;
        isReadonly: boolean;
        isEnable: boolean;
    },
    boolean
>; // { name: string; count: number }

English

From T, pick a set of properties whose type are not assignable to U.

For Example

type OmitBoolean = OmitByType<
    {
        name: string;
        count: number;
        isReadonly: boolean;
        isEnable: boolean;
    },
    boolean
>; // { name: string; count: number }

答案

type OmitByType<T, U> = { [K in keyof T as T[K] extends U ? never : K]: T[K] };

在线演示

posted @ 2022-09-22 20:45  Laggage  阅读(32)  评论(0编辑  收藏  举报