ts keys类型枚举

// 从 DataView 类型中取出key
type get_set = keyof Omit<DataView, "buffer" | "byteLength" | "byteOffset">;

type FilterNotStartingWith<Set, Needle extends string> = Set extends `${Needle}${infer _X}` ? never : Set

// 从get_set 中取出以 set字符串开始的成员
type set = FilterNotStartingWith<get_set, "get">
type set2 = Exclude<get_set, `get${string}`>
type set3 = Extract<get_set, `set${string}`>

// 从get_set 中取出以 get字符串开始的成员
type get = FilterNotStartingWith<get_set, "set">
type get2 =  Exclude<get_set, `set${string}`>
type get3 =  Extract<get_set, `get${string}`>

还有种简单的办法

// type setkeys = "setFloat32" | "setFloat64" | "setInt8" | "setInt16" | "setInt32" | "setUint8" | "setUint16" | "setUint32" | "setBigInt64" | "setBigUint64"
type setkeys = Extract<keyof DataView, `set${string}`>;

// type getkeys = "getFloat32" | "getFloat64" | "getInt8" | "getInt16" | "getInt32" | "getUint8" | "getUint16" | "getUint32" | "getBigInt64" | "getBigUint64"
type getkeys = Extract<keyof DataView, `get${string}`>;
posted @ 2022-11-14 18:13  Ajanuw  阅读(267)  评论(0编辑  收藏  举报