Skip to content

Implement type narrowing for Array.prototype.includes #58988

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib/es2016.array.include.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface Array<T> {
* @param searchElement The element to search for.
* @param fromIndex The position in this array at which to begin searching for searchElement.
*/
includes(searchElement: T, fromIndex?: number): boolean;
includes<U>(searchElement: U, fromIndex?: number): searchElement is T extends U ? T : never;
}

interface ReadonlyArray<T> {
Expand All @@ -13,7 +13,7 @@ interface ReadonlyArray<T> {
* @param searchElement The element to search for.
* @param fromIndex The position in this array at which to begin searching for searchElement.
*/
includes(searchElement: T, fromIndex?: number): boolean;
includes<U>(searchElement: U, fromIndex?: number): searchElement is T extends U ? T : never;
}

interface Int8Array {
Expand Down
32 changes: 32 additions & 0 deletions tests/baselines/reference/arrayIncludes.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
arrayIncludes.ts(10,12): error TS2339: Property 'length' does not exist on type 'never'.
arrayIncludes.ts(21,12): error TS2339: Property 'length' does not exist on type 'never'.


==== arrayIncludes.ts (2 errors) ====
const array1 = ['foo', 'bar'] as const;

const value1: string = 'baz';
if (array1.includes(value1)) {
value1.length;
}

const value2 = 'baz';
if (array1.includes(value2)) {
value2.length;
~~~~~~
!!! error TS2339: Property 'length' does not exist on type 'never'.
}

const value3 = 'bar';
if (array1.includes(value3)) {
value3.length;
}

const value4 = 'baz';
const result0 = array1.includes(value4);
if (result0) {
value4.length;
~~~~~~
!!! error TS2339: Property 'length' does not exist on type 'never'.
}

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

//// [arrayIncludes.ts]
const array1 = ['foo', 'bar'] as const;

const value1: string = 'baz';
if (array1.includes(value1)) {
value1.length;
}

const value2 = 'baz';
if (array1.includes(value2)) {
value2.length;
}

const value3 = 'bar';
if (array1.includes(value3)) {
value3.length;
}

const value4 = 'baz';
const result0 = array1.includes(value4);
if (result0) {
value4.length;
}


//// [arrayIncludes.js]
const array1 = ['foo', 'bar'];
const value1 = 'baz';
if (array1.includes(value1)) {
value1.length;
}
const value2 = 'baz';
if (array1.includes(value2)) {
value2.length;
}
const value3 = 'bar';
if (array1.includes(value3)) {
value3.length;
}
const value4 = 'baz';
const result0 = array1.includes(value4);
if (result0) {
value4.length;
}
67 changes: 67 additions & 0 deletions tests/baselines/reference/arrayIncludes.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//// [tests/cases/compiler/arrayIncludes.ts] ////

=== arrayIncludes.ts ===
const array1 = ['foo', 'bar'] as const;
>array1 : Symbol(array1, Decl(arrayIncludes.ts, 0, 5))
>const : Symbol(const)

const value1: string = 'baz';
>value1 : Symbol(value1, Decl(arrayIncludes.ts, 2, 5))

if (array1.includes(value1)) {
>array1.includes : Symbol(ReadonlyArray.includes, Decl(lib.es2016.array.include.d.ts, --, --))
>array1 : Symbol(array1, Decl(arrayIncludes.ts, 0, 5))
>includes : Symbol(ReadonlyArray.includes, Decl(lib.es2016.array.include.d.ts, --, --))
>value1 : Symbol(value1, Decl(arrayIncludes.ts, 2, 5))

value1.length;
>value1.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>value1 : Symbol(value1, Decl(arrayIncludes.ts, 2, 5))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
}

const value2 = 'baz';
>value2 : Symbol(value2, Decl(arrayIncludes.ts, 7, 5))

if (array1.includes(value2)) {
>array1.includes : Symbol(ReadonlyArray.includes, Decl(lib.es2016.array.include.d.ts, --, --))
>array1 : Symbol(array1, Decl(arrayIncludes.ts, 0, 5))
>includes : Symbol(ReadonlyArray.includes, Decl(lib.es2016.array.include.d.ts, --, --))
>value2 : Symbol(value2, Decl(arrayIncludes.ts, 7, 5))

value2.length;
>value2 : Symbol(value2, Decl(arrayIncludes.ts, 7, 5))
}

const value3 = 'bar';
>value3 : Symbol(value3, Decl(arrayIncludes.ts, 12, 5))

if (array1.includes(value3)) {
>array1.includes : Symbol(ReadonlyArray.includes, Decl(lib.es2016.array.include.d.ts, --, --))
>array1 : Symbol(array1, Decl(arrayIncludes.ts, 0, 5))
>includes : Symbol(ReadonlyArray.includes, Decl(lib.es2016.array.include.d.ts, --, --))
>value3 : Symbol(value3, Decl(arrayIncludes.ts, 12, 5))

value3.length;
>value3.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>value3 : Symbol(value3, Decl(arrayIncludes.ts, 12, 5))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
}

const value4 = 'baz';
>value4 : Symbol(value4, Decl(arrayIncludes.ts, 17, 5))

const result0 = array1.includes(value4);
>result0 : Symbol(result0, Decl(arrayIncludes.ts, 18, 5))
>array1.includes : Symbol(ReadonlyArray.includes, Decl(lib.es2016.array.include.d.ts, --, --))
>array1 : Symbol(array1, Decl(arrayIncludes.ts, 0, 5))
>includes : Symbol(ReadonlyArray.includes, Decl(lib.es2016.array.include.d.ts, --, --))
>value4 : Symbol(value4, Decl(arrayIncludes.ts, 17, 5))

if (result0) {
>result0 : Symbol(result0, Decl(arrayIncludes.ts, 18, 5))

value4.length;
>value4 : Symbol(value4, Decl(arrayIncludes.ts, 17, 5))
}

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

=== arrayIncludes.ts ===
const array1 = ['foo', 'bar'] as const;
>array1 : readonly ["foo", "bar"]
> : ^^^^^^^^^^^^^^^^^^^^^^^
>['foo', 'bar'] as const : readonly ["foo", "bar"]
> : ^^^^^^^^^^^^^^^^^^^^^^^
>['foo', 'bar'] : readonly ["foo", "bar"]
> : ^^^^^^^^^^^^^^^^^^^^^^^
>'foo' : "foo"
> : ^^^^^
>'bar' : "bar"
> : ^^^^^

const value1: string = 'baz';
>value1 : string
> : ^^^^^^
>'baz' : "baz"
> : ^^^^^

if (array1.includes(value1)) {
>array1.includes(value1) : boolean
> : ^^^^^^^
>array1.includes : <U>(searchElement: U, fromIndex?: number) => searchElement is ("foo" extends infer T ? T extends "foo" ? T extends U ? U & ("foo" | "bar") : never : never : never) | ("bar" extends infer T_1 ? T_1 extends "bar" ? T_1 extends U ? U & ("foo" | "bar") : never : never : never)
> : ^ ^^ ^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>array1 : readonly ["foo", "bar"]
> : ^^^^^^^^^^^^^^^^^^^^^^^
>includes : <U>(searchElement: U, fromIndex?: number) => searchElement is ("foo" extends infer T ? T extends "foo" ? T extends U ? U & ("foo" | "bar") : never : never : never) | ("bar" extends infer T_1 ? T_1 extends "bar" ? T_1 extends U ? U & ("foo" | "bar") : never : never : never)
> : ^ ^^ ^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>value1 : string
> : ^^^^^^

value1.length;
>value1.length : number
> : ^^^^^^
>value1 : "foo" | "bar"
> : ^^^^^^^^^^^^^
>length : number
> : ^^^^^^
}

const value2 = 'baz';
>value2 : "baz"
> : ^^^^^
>'baz' : "baz"
> : ^^^^^

if (array1.includes(value2)) {
>array1.includes(value2) : boolean
> : ^^^^^^^
>array1.includes : <U>(searchElement: U, fromIndex?: number) => searchElement is ("foo" extends infer T ? T extends "foo" ? T extends U ? U & ("foo" | "bar") : never : never : never) | ("bar" extends infer T_1 ? T_1 extends "bar" ? T_1 extends U ? U & ("foo" | "bar") : never : never : never)
> : ^ ^^ ^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>array1 : readonly ["foo", "bar"]
> : ^^^^^^^^^^^^^^^^^^^^^^^
>includes : <U>(searchElement: U, fromIndex?: number) => searchElement is ("foo" extends infer T ? T extends "foo" ? T extends U ? U & ("foo" | "bar") : never : never : never) | ("bar" extends infer T_1 ? T_1 extends "bar" ? T_1 extends U ? U & ("foo" | "bar") : never : never : never)
> : ^ ^^ ^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>value2 : "baz"
> : ^^^^^

value2.length;
>value2.length : any
> : ^^^
>value2 : never
> : ^^^^^
>length : any
> : ^^^
}

const value3 = 'bar';
>value3 : "bar"
> : ^^^^^
>'bar' : "bar"
> : ^^^^^

if (array1.includes(value3)) {
>array1.includes(value3) : boolean
> : ^^^^^^^
>array1.includes : <U>(searchElement: U, fromIndex?: number) => searchElement is ("foo" extends infer T ? T extends "foo" ? T extends U ? U & ("foo" | "bar") : never : never : never) | ("bar" extends infer T_1 ? T_1 extends "bar" ? T_1 extends U ? U & ("foo" | "bar") : never : never : never)
> : ^ ^^ ^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>array1 : readonly ["foo", "bar"]
> : ^^^^^^^^^^^^^^^^^^^^^^^
>includes : <U>(searchElement: U, fromIndex?: number) => searchElement is ("foo" extends infer T ? T extends "foo" ? T extends U ? U & ("foo" | "bar") : never : never : never) | ("bar" extends infer T_1 ? T_1 extends "bar" ? T_1 extends U ? U & ("foo" | "bar") : never : never : never)
> : ^ ^^ ^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>value3 : "bar"
> : ^^^^^

value3.length;
>value3.length : number
> : ^^^^^^
>value3 : "bar"
> : ^^^^^
>length : number
> : ^^^^^^
}

const value4 = 'baz';
>value4 : "baz"
> : ^^^^^
>'baz' : "baz"
> : ^^^^^

const result0 = array1.includes(value4);
>result0 : boolean
> : ^^^^^^^
>array1.includes(value4) : boolean
> : ^^^^^^^
>array1.includes : <U>(searchElement: U, fromIndex?: number) => searchElement is ("foo" extends infer T ? T extends "foo" ? T extends U ? U & ("foo" | "bar") : never : never : never) | ("bar" extends infer T_1 ? T_1 extends "bar" ? T_1 extends U ? U & ("foo" | "bar") : never : never : never)
> : ^ ^^ ^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>array1 : readonly ["foo", "bar"]
> : ^^^^^^^^^^^^^^^^^^^^^^^
>includes : <U>(searchElement: U, fromIndex?: number) => searchElement is ("foo" extends infer T ? T extends "foo" ? T extends U ? U & ("foo" | "bar") : never : never : never) | ("bar" extends infer T_1 ? T_1 extends "bar" ? T_1 extends U ? U & ("foo" | "bar") : never : never : never)
> : ^ ^^ ^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>value4 : "baz"
> : ^^^^^

if (result0) {
>result0 : boolean
> : ^^^^^^^

value4.length;
>value4.length : any
> : ^^^
>value4 : never
> : ^^^^^
>length : any
> : ^^^
}

Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ function getImplicitAriaRole(element: SomeRecord) {
> : ^^^^^^^
>parents.includes(parent.a) : boolean
> : ^^^^^^^
>parents.includes : (searchElement: string, fromIndex?: number) => boolean
> : ^ ^^^^^^^^^^ ^^^ ^^^^^
>parents.includes : <U>(searchElement: U, fromIndex?: number) => searchElement is string extends infer T ? T extends string ? T extends U ? U & string : never : never : never
> : ^ ^^ ^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>parents : string[]
> : ^^^^^^^^
>includes : (searchElement: string, fromIndex?: number) => boolean
> : ^ ^^^^^^^^^^ ^^^ ^^^^^
>includes : <U>(searchElement: U, fromIndex?: number) => searchElement is string extends infer T ? T extends string ? T extends U ? U & string : never : never : never
> : ^ ^^ ^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>parent.a : string
> : ^^^^^^
>parent : SomeRecord
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//// [tests/cases/compiler/excessiveStackDepthFlatArray.ts] ////

=== Performance Stats ===
Instantiation count: 1,000
Instantiation count: 2,500

=== index.tsx ===
interface MiddlewareArray<T> extends Array<T> {}
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/keyofAndIndexedAccess2.types
Original file line number Diff line number Diff line change
Expand Up @@ -649,12 +649,12 @@ export function getEntity<E extends Entity>(id: IdOf<E>, state: EntityState<E>):
> : ^^^^^^^
>ids.includes(id) : boolean
> : ^^^^^^^
>ids.includes : (searchElement: IdOf<E>, fromIndex?: number) => boolean
> : ^ ^^^^^^^^^^^ ^^^ ^^^^^
>ids.includes : <U>(searchElement: U, fromIndex?: number) => searchElement is IdOf<E> extends infer T ? T extends IdOf<E> ? T extends U ? IdOf<E> : never : never : never
> : ^ ^^ ^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>ids : IdOf<E>[]
> : ^^^^^^^^^
>includes : (searchElement: IdOf<E>, fromIndex?: number) => boolean
> : ^ ^^^^^^^^^^^ ^^^ ^^^^^
>includes : <U>(searchElement: U, fromIndex?: number) => searchElement is IdOf<E> extends infer T ? T extends IdOf<E> ? T extends U ? IdOf<E> : never : never : never
> : ^ ^^ ^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>id : IdOf<E>
> : ^^^^^^^

Expand Down
Loading
Loading