Skip to content

Commit 38cb69b

Browse files
committed
Fix unsound Array.flatMap() typings
1 parent 70b902e commit 38cb69b

7 files changed

+41
-505
lines changed

src/lib/es2019.array.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ interface ReadonlyArray<T> {
1111
* thisArg is omitted, undefined is used as the this value.
1212
*/
1313
flatMap<U, This = undefined> (
14-
callback: (this: This, value: T, index: number, array: T[]) => U | ReadonlyArray<U>,
14+
callback: (this: This, value: T, index: number, array: T[]) => U,
1515
thisArg?: This
16-
): U[]
16+
): (U extends readonly (infer V)[] ? V : U)[];
1717

1818

1919
/**
@@ -125,9 +125,9 @@ interface Array<T> {
125125
* thisArg is omitted, undefined is used as the this value.
126126
*/
127127
flatMap<U, This = undefined> (
128-
callback: (this: This, value: T, index: number, array: T[]) => U | ReadonlyArray<U>,
128+
callback: (this: This, value: T, index: number, array: T[]) => U,
129129
thisArg?: This
130-
): U[]
130+
): (U extends readonly (infer V)[] ? V : U)[];
131131

132132
/**
133133
* Returns a new array with all sub-array elements concatenated into it recursively up to the

tests/baselines/reference/arrayFlatMap.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ const readonlyArray: ReadonlyArray<number> = [];
99

1010
array.flatMap((): ReadonlyArray<number> => []); // ok
1111
>array.flatMap((): ReadonlyArray<number> => []) : number[]
12-
>array.flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
12+
>array.flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U, thisArg?: This) => (U extends readonly (infer V)[] ? V : U)[]
1313
>array : number[]
14-
>flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
14+
>flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U, thisArg?: This) => (U extends readonly (infer V)[] ? V : U)[]
1515
>(): ReadonlyArray<number> => [] : () => readonly number[]
1616
>[] : undefined[]
1717

1818
readonlyArray.flatMap((): ReadonlyArray<number> => []); // ok
1919
>readonlyArray.flatMap((): ReadonlyArray<number> => []) : number[]
20-
>readonlyArray.flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
20+
>readonlyArray.flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U, thisArg?: This) => (U extends readonly (infer V)[] ? V : U)[]
2121
>readonlyArray : readonly number[]
22-
>flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
22+
>flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U, thisArg?: This) => (U extends readonly (infer V)[] ? V : U)[]
2323
>(): ReadonlyArray<number> => [] : () => readonly number[]
2424
>[] : undefined[]
2525

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Exit Code: 1
2+
Standard output:
3+
src/components/Hello.spec.ts(4,1): error TS2593: Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig.
4+
src/components/Hello.spec.ts(5,3): error TS2593: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig.
5+
src/components/Hello.spec.ts(14,5): error TS2304: Cannot find name 'expect'.
6+
7+
8+
9+
Standard error:

tests/baselines/reference/user/TypeScript-WeChat-Starter.log

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
Exit Code: 1
22
Standard output:
3-
../../../../../node_modules/@types/mocha/index.d.ts(2680,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'beforeEach' must be of type 'Lifecycle', but here has type 'HookFunction'.
4-
../../../../../node_modules/@types/mocha/index.d.ts(2698,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'afterEach' must be of type 'Lifecycle', but here has type 'HookFunction'.
5-
../../../../../node_modules/@types/mocha/index.d.ts(2714,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'describe' must be of type 'Describe', but here has type 'SuiteFunction'.
6-
../../../../../node_modules/@types/mocha/index.d.ts(2735,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'xdescribe' must be of type 'Describe', but here has type 'PendingSuiteFunction'.
7-
../../../../../node_modules/@types/mocha/index.d.ts(2749,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'it' must be of type 'It', but here has type 'TestFunction'.
8-
../../../../../node_modules/@types/mocha/index.d.ts(2763,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'test' must be of type 'It', but here has type 'TestFunction'.
9-
../../../../../node_modules/@types/mocha/index.d.ts(2770,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'xit' must be of type 'It', but here has type 'PendingTestFunction'.
3+
src/App.test.tsx(5,1): error TS2593: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig.
4+
src/components/Hello.test.tsx(5,1): error TS2593: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig.
5+
src/components/Hello.test.tsx(7,3): error TS2304: Cannot find name 'expect'.
6+
src/components/Hello.test.tsx(10,1): error TS2593: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig.
7+
src/components/Hello.test.tsx(12,3): error TS2304: Cannot find name 'expect'.
8+
src/components/Hello.test.tsx(15,1): error TS2593: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig.
9+
src/components/Hello.test.tsx(17,3): error TS2304: Cannot find name 'expect'.
10+
src/components/Hello.test.tsx(20,1): error TS2593: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig.
11+
src/components/Hello.test.tsx(21,3): error TS2304: Cannot find name 'expect'.
12+
src/components/Hello.test.tsx(26,1): error TS2593: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig.
13+
src/components/Hello.test.tsx(27,3): error TS2304: Cannot find name 'expect'.
1014

1115

1216

0 commit comments

Comments
 (0)