我的目标是在TypeScript中编写满足以下条件的谓词函数(例如isNull
和isUndefined
):
array.filter(isNull)
array.filter(and(not(isNull), not(isUndefined)))
array.filter(isNull)
的返回类型将是null[]
const isNotNull = not(isNull)
前两个条件很容易满足:
type Predicate = (i: any) => boolean;
const and = (p1: Predicate, p2: Predicate) =>
(i: any) => p1(i) && p2(i);
const or = (p1: Predicate, p2: Predicate) =>
(i: any) => p1(i) || p2(i);
const not = (p: Predicate) =>
(i: any) => !p(i);
const isNull = (i: any) =>
i === null;
const isUndefined = (i: any) =>
i === undefined;
const items = [ "foo", null, 123, undefined, true ];
const filtered = items.filter(and(not(isNull), not(isUndefined)));
console.log(filtered);
但是由于此处未使用类型保护,因此TypeScript假定变量filtered
与items
的类型与(string,number,boolean,null,undefined)[]
具有相同的类型,而实际上它现在应该是(string,number,boolean)[]
所以我添加了一些打字魔术:
type Diff<T, U> = T extends U ? never : T;
type Predicate<I, O extends I> = (i: I) => i is O;
const and = <I, O1 extends I, O2 extends I>(p1: Predicate<I, O1>, p2: Predicate<I, O2>) =>
(i: I): i is (O1 & O2) => p1(i) && p2(i);
const or = <I, O1 extends I, O2 extends I>(p1: Predicate<I, O1>, p2: Predicate<I, O2>) =>
(i: I): i is (O1 | O2) => p1(i) || p2(i);
const not = <I, O extends I>(p: Predicate<I, O>) =>
(i: I): i is (Diff<I, O>) => !p(i);
const isNull = <I>(i: I | null): i is null =>
i === null;
const isUndefined = <I>(i: I | undefined): i is undefined =>
i === undefined;
现在看来可行,filtered
已正确还原为类型(string,number,boolean)[]
。
但是因为not(isNull)
可能经常使用,所以我想将其提取到新的谓词函数中:
const isNotNull = not(isNull);
尽管不幸的是,它在运行时完美运行,但无法编译(启用严格模式的TypeScript 3.3.3):
Argument of type '<I>(i: I | null) => i is null' is not assignable to parameter of type 'Predicate<{}, {}>'.
Type predicate 'i is null' is not assignable to 'i is {}'.
Type 'null' is not assignable to type '{}'.ts(2345)
因此,我猜想在使用谓词作为数组filter
的参数时,TypeScript可以从数组中推断I
的类型,但是当将谓词提取到单独的函数中时,它将不再起作用,并且TypeScript退回到基础对象类型{}
,这破坏了一切。
是否可以解决此问题?说服TypeScript在定义I
函数时坚持使用通用类型{}
而不是将其解析为isNotNull
的一些技巧?还是这是TypeScript的限制,目前无法完成?
答案 0 :(得分:2)
刚刚在这里找到了我自己两年前的问题,并使用最新的 TypeScript 版本 (4.3.5) 再次尝试,问题不再存在。以下代码编译正常,类型正确推断:
type Diff<T, U> = T extends U ? never : T;
type Predicate<I, O extends I> = (i: I) => i is O;
const and = <I, O1 extends I, O2 extends I>(p1: Predicate<I, O1>, p2: Predicate<I, O2>) =>
(i: I): i is (O1 & O2) => p1(i) && p2(i);
const or = <I, O1 extends I, O2 extends I>(p1: Predicate<I, O1>, p2: Predicate<I, O2>) =>
(i: I): i is (O1 | O2) => p1(i) || p2(i);
const not = <I, O extends I>(p: Predicate<I, O>) =>
(i: I): i is (Diff<I, O>) => !p(i);
const isNull = <I>(i: I | null): i is null =>
i === null;
const isUndefined = <I>(i: I | undefined): i is undefined =>
i === undefined;
const isNotNull = not(isNull);
const isNotUndefined = not(isUndefined);
const items = [ "foo", null, 123, undefined, true ];
const filtered = items.filter(and(isNotNull, isNotUndefined));
console.log(filtered);
答案 1 :(得分:0)
这是您要找的吗?
const isNotNull = not(<Predicate<any, null>>isNull);
答案 2 :(得分:0)
从上下文传递类型信息。该代码编译良好 p>
// c: (string | number)[]
let c = [1, 2, 'b', 'a', null].filter(not<number | string | null, null>(isNull));