Skip to content

Commit

Permalink
original checker and new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
craigphicks committed Nov 9, 2023
1 parent 3e12250 commit 79f69d5
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/baselines/reference/arrayFilterBooleanOverload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//// [tests/cases/compiler/arrayFilterBooleanOverload.ts] ////

//// [arrayFilterBooleanOverload.ts]
const nullableValues = ['a', 'b', null]; // expect (string | null)[]

const values1 = nullableValues.filter(Boolean); // expect string[]

// @ts-expect-error
const values2 = nullableValues.filter(new Boolean);

const arr = [0, 1, "", "foo", null] as const;

const arr2 = arr.filter(Boolean); // expect ("foo" | 1)[]



//// [arrayFilterBooleanOverload.js]
"use strict";
const nullableValues = ['a', 'b', null]; // expect (string | null)[]
const values1 = nullableValues.filter(Boolean); // expect string[]
// @ts-expect-error
const values2 = nullableValues.filter(new Boolean);
const arr = [0, 1, "", "foo", null];
const arr2 = arr.filter(Boolean); // expect ("foo" | 1)[]


//// [arrayFilterBooleanOverload.d.ts]
declare const nullableValues: (string | null)[];
declare const values1: (string | null)[];
declare const values2: (string | null)[];
declare const arr: readonly [0, 1, "", "foo", null];
declare const arr2: ("" | 0 | 1 | "foo" | null)[];
33 changes: 33 additions & 0 deletions tests/baselines/reference/arrayFilterBooleanOverload.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//// [tests/cases/compiler/arrayFilterBooleanOverload.ts] ////

=== arrayFilterBooleanOverload.ts ===
const nullableValues = ['a', 'b', null]; // expect (string | null)[]
>nullableValues : Symbol(nullableValues, Decl(arrayFilterBooleanOverload.ts, 0, 5))

const values1 = nullableValues.filter(Boolean); // expect string[]
>values1 : Symbol(values1, Decl(arrayFilterBooleanOverload.ts, 2, 5))
>nullableValues.filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>nullableValues : Symbol(nullableValues, Decl(arrayFilterBooleanOverload.ts, 0, 5))
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Boolean : Symbol(Boolean, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

// @ts-expect-error
const values2 = nullableValues.filter(new Boolean);
>values2 : Symbol(values2, Decl(arrayFilterBooleanOverload.ts, 5, 5))
>nullableValues.filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>nullableValues : Symbol(nullableValues, Decl(arrayFilterBooleanOverload.ts, 0, 5))
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Boolean : Symbol(Boolean, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

const arr = [0, 1, "", "foo", null] as const;
>arr : Symbol(arr, Decl(arrayFilterBooleanOverload.ts, 7, 5))
>const : Symbol(const)

const arr2 = arr.filter(Boolean); // expect ("foo" | 1)[]
>arr2 : Symbol(arr2, Decl(arrayFilterBooleanOverload.ts, 9, 5))
>arr.filter : Symbol(ReadonlyArray.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>arr : Symbol(arr, Decl(arrayFilterBooleanOverload.ts, 7, 5))
>filter : Symbol(ReadonlyArray.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Boolean : Symbol(Boolean, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))


45 changes: 45 additions & 0 deletions tests/baselines/reference/arrayFilterBooleanOverload.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//// [tests/cases/compiler/arrayFilterBooleanOverload.ts] ////

=== arrayFilterBooleanOverload.ts ===
const nullableValues = ['a', 'b', null]; // expect (string | null)[]
>nullableValues : (string | null)[]
>['a', 'b', null] : (string | null)[]
>'a' : "a"
>'b' : "b"

const values1 = nullableValues.filter(Boolean); // expect string[]
>values1 : (string | null)[]
>nullableValues.filter(Boolean) : (string | null)[]
>nullableValues.filter : { <S extends string | null>(predicate: (value: string | null, index: number, array: (string | null)[]) => value is S, thisArg?: any): S[]; (predicate: (value: string | null, index: number, array: (string | null)[]) => unknown, thisArg?: any): (string | null)[]; }
>nullableValues : (string | null)[]
>filter : { <S extends string | null>(predicate: (value: string | null, index: number, array: (string | null)[]) => value is S, thisArg?: any): S[]; (predicate: (value: string | null, index: number, array: (string | null)[]) => unknown, thisArg?: any): (string | null)[]; }
>Boolean : BooleanConstructor

// @ts-expect-error
const values2 = nullableValues.filter(new Boolean);
>values2 : (string | null)[]
>nullableValues.filter(new Boolean) : (string | null)[]
>nullableValues.filter : { <S extends string | null>(predicate: (value: string | null, index: number, array: (string | null)[]) => value is S, thisArg?: any): S[]; (predicate: (value: string | null, index: number, array: (string | null)[]) => unknown, thisArg?: any): (string | null)[]; }
>nullableValues : (string | null)[]
>filter : { <S extends string | null>(predicate: (value: string | null, index: number, array: (string | null)[]) => value is S, thisArg?: any): S[]; (predicate: (value: string | null, index: number, array: (string | null)[]) => unknown, thisArg?: any): (string | null)[]; }
>new Boolean : Boolean
>Boolean : BooleanConstructor

const arr = [0, 1, "", "foo", null] as const;
>arr : readonly [0, 1, "", "foo", null]
>[0, 1, "", "foo", null] as const : readonly [0, 1, "", "foo", null]
>[0, 1, "", "foo", null] : readonly [0, 1, "", "foo", null]
>0 : 0
>1 : 1
>"" : ""
>"foo" : "foo"

const arr2 = arr.filter(Boolean); // expect ("foo" | 1)[]
>arr2 : ("" | 0 | 1 | "foo" | null)[]
>arr.filter(Boolean) : ("" | 0 | 1 | "foo" | null)[]
>arr.filter : { <S extends "" | 0 | 1 | "foo" | null>(predicate: (value: "" | 0 | 1 | "foo" | null, index: number, array: readonly ("" | 0 | 1 | "foo" | null)[]) => value is S, thisArg?: any): S[]; (predicate: (value: "" | 0 | 1 | "foo" | null, index: number, array: readonly ("" | 0 | 1 | "foo" | null)[]) => unknown, thisArg?: any): ("" | 0 | 1 | "foo" | null)[]; }
>arr : readonly [0, 1, "", "foo", null]
>filter : { <S extends "" | 0 | 1 | "foo" | null>(predicate: (value: "" | 0 | 1 | "foo" | null, index: number, array: readonly ("" | 0 | 1 | "foo" | null)[]) => value is S, thisArg?: any): S[]; (predicate: (value: "" | 0 | 1 | "foo" | null, index: number, array: readonly ("" | 0 | 1 | "foo" | null)[]) => unknown, thisArg?: any): ("" | 0 | 1 | "foo" | null)[]; }
>Boolean : BooleanConstructor


38 changes: 38 additions & 0 deletions tests/baselines/reference/unionOfArraysBooleanFilterCall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//// [tests/cases/compiler/unionOfArraysBooleanFilterCall.ts] ////

//// [unionOfArraysBooleanFilterCall.ts]
interface Fizz {
id: number;
member: number;
}
interface Buzz {
id: number;
member: string;
}
type Falsey = "" | 0 | false | null | undefined;


([] as (Fizz|Falsey)[] | (Buzz|Falsey)[]).filter(Boolean); // expect type (Fizz|Buzz)[]

([] as (Fizz|Falsey)[] | readonly (Buzz|Falsey)[]).filter(Boolean); // expect type (Fizz|Buzz)[]

([] as [Fizz|Falsey] | readonly [(Buzz|Falsey)?]).filter(Boolean); // expect type (Fizz|Buzz)[]

// confirm that the other filter signatures are still available and working

declare function isFizz(x: unknown): x is Fizz;
([] as (Fizz|Falsey)[] | (Buzz|Falsey)[]).filter(isFizz); // expect type Fizz[]

declare function isBuzz(x: unknown): x is Buzz;
([] as (Fizz|Falsey)[] | (Buzz|Falsey)[]).filter(isBuzz); // expect type Buzz[]

([] as (Fizz|Falsey)[] | (Buzz|Falsey)[]).filter(x => x && typeof x.member === "number"); // expect type (Fizz|Buzz|Falsey)[]


//// [unionOfArraysBooleanFilterCall.js]
[].filter(Boolean); // expect type (Fizz|Buzz)[]
[].filter(Boolean); // expect type (Fizz|Buzz)[]
[].filter(Boolean); // expect type (Fizz|Buzz)[]
[].filter(isFizz); // expect type Fizz[]
[].filter(isBuzz); // expect type Buzz[]
[].filter(x => x && typeof x.member === "number"); // expect type (Fizz|Buzz|Falsey)[]
97 changes: 97 additions & 0 deletions tests/baselines/reference/unionOfArraysBooleanFilterCall.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//// [tests/cases/compiler/unionOfArraysBooleanFilterCall.ts] ////

=== unionOfArraysBooleanFilterCall.ts ===
interface Fizz {
>Fizz : Symbol(Fizz, Decl(unionOfArraysBooleanFilterCall.ts, 0, 0))

id: number;
>id : Symbol(Fizz.id, Decl(unionOfArraysBooleanFilterCall.ts, 0, 16))

member: number;
>member : Symbol(Fizz.member, Decl(unionOfArraysBooleanFilterCall.ts, 1, 15))
}
interface Buzz {
>Buzz : Symbol(Buzz, Decl(unionOfArraysBooleanFilterCall.ts, 3, 1))

id: number;
>id : Symbol(Buzz.id, Decl(unionOfArraysBooleanFilterCall.ts, 4, 16))

member: string;
>member : Symbol(Buzz.member, Decl(unionOfArraysBooleanFilterCall.ts, 5, 15))
}
type Falsey = "" | 0 | false | null | undefined;
>Falsey : Symbol(Falsey, Decl(unionOfArraysBooleanFilterCall.ts, 7, 1))


([] as (Fizz|Falsey)[] | (Buzz|Falsey)[]).filter(Boolean); // expect type (Fizz|Buzz)[]
>([] as (Fizz|Falsey)[] | (Buzz|Falsey)[]).filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Fizz : Symbol(Fizz, Decl(unionOfArraysBooleanFilterCall.ts, 0, 0))
>Falsey : Symbol(Falsey, Decl(unionOfArraysBooleanFilterCall.ts, 7, 1))
>Buzz : Symbol(Buzz, Decl(unionOfArraysBooleanFilterCall.ts, 3, 1))
>Falsey : Symbol(Falsey, Decl(unionOfArraysBooleanFilterCall.ts, 7, 1))
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Boolean : Symbol(Boolean, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

([] as (Fizz|Falsey)[] | readonly (Buzz|Falsey)[]).filter(Boolean); // expect type (Fizz|Buzz)[]
>([] as (Fizz|Falsey)[] | readonly (Buzz|Falsey)[]).filter : Symbol(filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Fizz : Symbol(Fizz, Decl(unionOfArraysBooleanFilterCall.ts, 0, 0))
>Falsey : Symbol(Falsey, Decl(unionOfArraysBooleanFilterCall.ts, 7, 1))
>Buzz : Symbol(Buzz, Decl(unionOfArraysBooleanFilterCall.ts, 3, 1))
>Falsey : Symbol(Falsey, Decl(unionOfArraysBooleanFilterCall.ts, 7, 1))
>filter : Symbol(filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Boolean : Symbol(Boolean, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

([] as [Fizz|Falsey] | readonly [(Buzz|Falsey)?]).filter(Boolean); // expect type (Fizz|Buzz)[]
>([] as [Fizz|Falsey] | readonly [(Buzz|Falsey)?]).filter : Symbol(filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Fizz : Symbol(Fizz, Decl(unionOfArraysBooleanFilterCall.ts, 0, 0))
>Falsey : Symbol(Falsey, Decl(unionOfArraysBooleanFilterCall.ts, 7, 1))
>Buzz : Symbol(Buzz, Decl(unionOfArraysBooleanFilterCall.ts, 3, 1))
>Falsey : Symbol(Falsey, Decl(unionOfArraysBooleanFilterCall.ts, 7, 1))
>filter : Symbol(filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Boolean : Symbol(Boolean, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

// confirm that the other filter signatures are still available and working

declare function isFizz(x: unknown): x is Fizz;
>isFizz : Symbol(isFizz, Decl(unionOfArraysBooleanFilterCall.ts, 15, 66))
>x : Symbol(x, Decl(unionOfArraysBooleanFilterCall.ts, 19, 24))
>x : Symbol(x, Decl(unionOfArraysBooleanFilterCall.ts, 19, 24))
>Fizz : Symbol(Fizz, Decl(unionOfArraysBooleanFilterCall.ts, 0, 0))

([] as (Fizz|Falsey)[] | (Buzz|Falsey)[]).filter(isFizz); // expect type Fizz[]
>([] as (Fizz|Falsey)[] | (Buzz|Falsey)[]).filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Fizz : Symbol(Fizz, Decl(unionOfArraysBooleanFilterCall.ts, 0, 0))
>Falsey : Symbol(Falsey, Decl(unionOfArraysBooleanFilterCall.ts, 7, 1))
>Buzz : Symbol(Buzz, Decl(unionOfArraysBooleanFilterCall.ts, 3, 1))
>Falsey : Symbol(Falsey, Decl(unionOfArraysBooleanFilterCall.ts, 7, 1))
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>isFizz : Symbol(isFizz, Decl(unionOfArraysBooleanFilterCall.ts, 15, 66))

declare function isBuzz(x: unknown): x is Buzz;
>isBuzz : Symbol(isBuzz, Decl(unionOfArraysBooleanFilterCall.ts, 20, 57))
>x : Symbol(x, Decl(unionOfArraysBooleanFilterCall.ts, 22, 24))
>x : Symbol(x, Decl(unionOfArraysBooleanFilterCall.ts, 22, 24))
>Buzz : Symbol(Buzz, Decl(unionOfArraysBooleanFilterCall.ts, 3, 1))

([] as (Fizz|Falsey)[] | (Buzz|Falsey)[]).filter(isBuzz); // expect type Buzz[]
>([] as (Fizz|Falsey)[] | (Buzz|Falsey)[]).filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Fizz : Symbol(Fizz, Decl(unionOfArraysBooleanFilterCall.ts, 0, 0))
>Falsey : Symbol(Falsey, Decl(unionOfArraysBooleanFilterCall.ts, 7, 1))
>Buzz : Symbol(Buzz, Decl(unionOfArraysBooleanFilterCall.ts, 3, 1))
>Falsey : Symbol(Falsey, Decl(unionOfArraysBooleanFilterCall.ts, 7, 1))
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>isBuzz : Symbol(isBuzz, Decl(unionOfArraysBooleanFilterCall.ts, 20, 57))

([] as (Fizz|Falsey)[] | (Buzz|Falsey)[]).filter(x => x && typeof x.member === "number"); // expect type (Fizz|Buzz|Falsey)[]
>([] as (Fizz|Falsey)[] | (Buzz|Falsey)[]).filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Fizz : Symbol(Fizz, Decl(unionOfArraysBooleanFilterCall.ts, 0, 0))
>Falsey : Symbol(Falsey, Decl(unionOfArraysBooleanFilterCall.ts, 7, 1))
>Buzz : Symbol(Buzz, Decl(unionOfArraysBooleanFilterCall.ts, 3, 1))
>Falsey : Symbol(Falsey, Decl(unionOfArraysBooleanFilterCall.ts, 7, 1))
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(unionOfArraysBooleanFilterCall.ts, 25, 49))
>x : Symbol(x, Decl(unionOfArraysBooleanFilterCall.ts, 25, 49))
>x.member : Symbol(member, Decl(unionOfArraysBooleanFilterCall.ts, 1, 15), Decl(unionOfArraysBooleanFilterCall.ts, 5, 15))
>x : Symbol(x, Decl(unionOfArraysBooleanFilterCall.ts, 25, 49))
>member : Symbol(member, Decl(unionOfArraysBooleanFilterCall.ts, 1, 15), Decl(unionOfArraysBooleanFilterCall.ts, 5, 15))

0 comments on commit 79f69d5

Please sign in to comment.