Skip to content
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 练习题第四十题 #60

Open
semlinker opened this issue Sep 25, 2021 · 5 comments
Open

「重学TS 2.0 」TS 练习题第四十题 #60

semlinker opened this issue Sep 25, 2021 · 5 comments

Comments

@semlinker
Copy link
Owner

实现 AnyOf 工具类型,只要数组中任意元素的类型非 Falsy 类型、 {} 类型或 [] 类型,则返回 true,否则返回 false。如果数组为空的话,则返回 false。具体的使用示例如下所示:

type AnyOf<T extends any[]> = // 你的实现代码

type A0 = AnyOf<[]>; // false
type A1 = AnyOf<[0, '', false, [], {}]> // false
type A2 = AnyOf<[1, "", false, [], {}]> // true

请在下面评论你的答案

@zhaoxiongfei
Copy link

type NotEmptyObject<T> = T extends {} ? ({} extends T ? false : true) : true;
type Flasy = 0 | "" | false | [];
type AnyOf<T extends any[]> = T extends [infer First, ...infer Rest]
  ? First extends Flasy
    ? AnyOf<Rest>
    : NotEmptyObject<First>
  : false;

type A0 = AnyOf<[]>; // false
type A1 = AnyOf<[0, "", false, [], {}]>; // false
type A2 = AnyOf<[1, "", false, [], {}]>; // true

@sunboyZgz
Copy link

写得烂了点

type FaslyUnion = 0 | "" | false | 0n | null | undefined;
type AnyOfUnion = FaslyUnion | [];
type IsEmptyObject<T> = T extends {} ? ({} extends T ? true : false) : false;

type AnyOf<T extends any[]> = Len<T> extends 0
  ? false
  : T[0] extends AnyOfUnion
  ? AnyOf<Tail<T>>
  : IsEmptyObject<T[0]> extends true
  ? false
  : true;
type A02 = AnyOf<[]>; // false
type A12 = AnyOf<[0, "", false, [], {}]>; // false
type A22 = AnyOf<[1, "", false, [], {}]>; // true

@zhaoxiongfei
Copy link

type NotEmptyObject<T> = T extends {} ? ({} extends T ? false : true) : true;
type Flasy = 0 | "" | false | [];
type AnyOf<T extends any[]> = T extends [infer First, ...infer Rest]
  ? [First] extends [Flasy]
    ? AnyOf<Rest>
    : NotEmptyObject<First>
  : false;

type A0 = AnyOf<[]>; // false
type A1 = AnyOf<[0, '', false, [], {}]> // false
type A2 = AnyOf<[1, "", false, [], {}]> // true
type A3 = AnyOf<[0, "" | 2, false, [], {}]> // true

注意 元组中存在联合类型的问题。因此比对空对象的时候[] 转换成元组去extends比对,避免联合类型发生分布执行

@mingzhans
Copy link

type Falsy = {
    [p in PropertyKey]: never
} | [] | '' | "" | false | 0 | undefined | null
type AnyOf<T extends any[]> = T extends [infer A, ...infer Rest] ? (A extends Falsy ? AnyOf<Rest> : true) : false// 你的实现代码

type A0 = AnyOf<[]>; // false
type A1 = AnyOf<[0, '', false, [], {}]> // false
type A2 = AnyOf<[1, "", false, [], {}]> // true// true

基本类型也都属于对象类型

@jackwangwj
Copy link

// 先extends infer,判断是否空数组
// [infer first, ...infer rest]
// 判断first 是否 extends 0, '' false , [], keyof {} 为 never
type Zero = T extends 0 ? true : false;
type T1 = Zero<0>

type Trim = T extends '' ? true : false;
type T2 = Trim<''>

type EmptyArr = T extends [] ? true : false
type T31 = EmptyArr<[]>
type T32 = EmptyArr<[1]>
type T33 = EmptyArr<'1'>
type T34 = EmptyArr<{}>

type EmptyObj = keyof T extends never ? true : false
type T4 = EmptyObj<{}>
type T41 = EmptyObj<[]>

type isFalsy = Zero extends true
? true
: Trim extends true
? true
: EmptyArr extends true
? true
: EmptyObj extends true
? true
: T extends false
? true :
false

type AnyOf<T extends any[]> = EmptyArr extends true
? false
: T extends [infer First, ...infer Rest]
? isFalsy extends true
? AnyOf
: true
: false

// 你的实现代码

type A0 = AnyOf<[]>; // false
type A1 = AnyOf<[0, '', false, [], {}]> // false
type A2 = AnyOf<[1, "", false, [], {}]> // true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants