-
Notifications
You must be signed in to change notification settings - Fork 391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
「重学TS 2.0 」TS 练习题第三十六题 #55
Comments
type Filter<T extends any[], F, A extends any[] = []> = T extends [infer Head, ...infer Tail]
? Head extends F
? Filter<Tail, F, [...A, Head]>
: Filter<Tail, F, A>
: A;
type F0 = Filter<[6, "lolo", 7, "semlinker", false], number>; // [6, 7]
type F1 = Filter<["kakuqo", 2, ["ts"], "lolo"], string>; // ["kakuqo", "lolo"]
type F2 = Filter<[0, true, any, "abao"], string>; // [any, "abao"] |
这题容易遗漏的地方是,需要判断当前类型是否为 any,其他没什么难度 // 判断当前类型是否为 any
type IsAny<T> = 0 extends (1 & T) ? true : false
// 实现一个 Filter 工具类型,用于根据类型变量 F 的值进行类型过滤。具体的使用示例如下所示:
type Filter<T extends any[], F> = T extends [infer R1, ...infer R2]
? IsAny<R1> extends true
? [R1, ...Filter<R2, F>]
: [...R1 extends F ? [R1] : [], ...Filter<R2, F>]
: []
type F0 = Filter<[6, "lolo", 7, "semlinker", false], number>; // [6, 7]
type F1 = Filter<["kakuqo", 2, ["ts"], "lolo"], string>; // ["kakuqo", "lolo"]
type F2 = Filter<[0, true, any, "abao"], string>; // [any, "abao"] |
type A0 = any & 1 // any
type A1 = any & boolean // any
type A2 = any & never // never |
type Filter<T extends any[], F, Result extends any[] = []> = T extends [
infer H,
...infer R
]
? [H] extends [F]
? Filter<R, F, [...Result, H]>
: Filter<R, F, [...Result]>
: Result;
type F0 = Filter<[6, "lolo", 7, "semlinker", false], number>; // [6, 7]
type F1 = Filter<["kakuqo", 2, ["ts"], "lolo"], string>; // ["kakuqo", "lolo"]
type F2 = Filter<[0, true, any, "abao"], string>; // [any, "abao"]
const test2: F2 = [1, "abao"]; |
type Filter<T extends any[], F> =
T extends [infer A, ...infer B] ? [A] extends [F] ? [A, ...Filter<B, F>] : Filter<B, F> : [] |
type Filter<T extends any[], F, R extends any[] = []> = T extends [infer A, ...infer B]
? [A] extends [F]
? Filter<B, F, [...R, A]>
: Filter<B, F, R>
: R;
type F0 = Filter<[6, "lolo", 7, "semlinker", false], number>; // [6, 7]
type F1 = Filter<["kakuqo", 2, ["ts"], "lolo"], string>; // ["kakuqo", "lolo"]
type F2 = Filter<[0, true, any, "abao"], string>; // [any, "abao"]
type F3 = Filter<[never, number | string, any, "abao"], string>; // [never, any, "abao"] 思路: 利用 extends [infer A, ...infer B] 来提取数组内的第一项,递归这样就能提取到全部的,之后判断的类型的时候转换成元组类型[A] extends [F] 这样能避免出现的联合类型分发执行的问题。 |
any extends others 会走true分支,也会走false分支 |
|
type Filter<T extends any[], F, U extends F[] = []> = T extends [infer A, ...infer Rest] type F0 = Filter<[6, "lolo", 7, "semlinker", false], number>; // [6, 7] |
type Filter<T extends any[], V> = T extends [infer F, ...infer R]
? [F] extends [V]
? [F, ...Filter<R, V>]
: Filter<R, V>
: []; |
|
|
type Filter<T extends any[], F, R extends F[] = []> = T extends [infer A, ...infer B] ?
Filter<B, F, [A] extends [F] ? [...R, A] : R>
: R
type F0 = Filter<[6, "lolo", 7, "semlinker", false], number>; // [6, 7]
type F1 = Filter<["kakuqo", 2, ["ts"], "lolo"], string>; // ["kakuqo", "lolo"]
type F2 = Filter<[0, true, any, "abao"], string>; // [any, "abao"] |
实现一个
Filter
工具类型,用于根据类型变量F
的值进行类型过滤。具体的使用示例如下所示:The text was updated successfully, but these errors were encountered: