Skip to content

fix first match in RegExpMatchArray being possibly undefined when noUncheckedIndexedAccess is enabled #49682

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

Merged
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: 4 additions & 0 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,10 @@ interface RegExpMatchArray extends Array<string> {
* A copy of the search string.
*/
input?: string;
/**
* The first match. This will always be present because `null` will be returned if there are no matches.
*/
0: string;
}

interface RegExpExecArray extends Array<string> {
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/reference/assignFromStringInterface2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ tests/cases/conformance/types/primitives/string/assignFromStringInterface2.ts(48
indexOf(searchString: string, position?: number): number;
lastIndexOf(searchString: string, position?: number): number;
localeCompare(that: string): number;
match(regexp: string): string[];
match(regexp: RegExp): string[];
match(regexp: string): RegExpMatchArray;
match(regexp: RegExp): RegExpMatchArray;
replace(searchValue: string, replaceValue: string): string;
replace(searchValue: string, replaceValue: (substring: string, ...args: any[]) => string): string;
replace(searchValue: RegExp, replaceValue: string): string;
Expand All @@ -44,11 +44,11 @@ tests/cases/conformance/types/primitives/string/assignFromStringInterface2.ts(48
var a: String;
var b: NotString;

a = x;
a = b;
a = x;
a = b;

b = a;
b = x;
b = a;
b = x;

x = a; // expected error
~
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/reference/assignFromStringInterface2.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ interface NotString {
indexOf(searchString: string, position?: number): number;
lastIndexOf(searchString: string, position?: number): number;
localeCompare(that: string): number;
match(regexp: string): string[];
match(regexp: RegExp): string[];
match(regexp: string): RegExpMatchArray;
match(regexp: RegExp): RegExpMatchArray;
replace(searchValue: string, replaceValue: string): string;
replace(searchValue: string, replaceValue: (substring: string, ...args: any[]) => string): string;
replace(searchValue: RegExp, replaceValue: string): string;
Expand All @@ -39,11 +39,11 @@ var x = '';
var a: String;
var b: NotString;

a = x;
a = b;
a = x;
a = b;

b = a;
b = x;
b = a;
b = x;

x = a; // expected error
x = b; // expected error
Expand Down
26 changes: 14 additions & 12 deletions tests/baselines/reference/assignFromStringInterface2.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,37 @@ interface NotString {
>localeCompare : Symbol(NotString.localeCompare, Decl(assignFromStringInterface2.ts, 11, 65))
>that : Symbol(that, Decl(assignFromStringInterface2.ts, 12, 18))

match(regexp: string): string[];
>match : Symbol(NotString.match, Decl(assignFromStringInterface2.ts, 12, 40), Decl(assignFromStringInterface2.ts, 13, 36))
match(regexp: string): RegExpMatchArray;
>match : Symbol(NotString.match, Decl(assignFromStringInterface2.ts, 12, 40), Decl(assignFromStringInterface2.ts, 13, 44))
>regexp : Symbol(regexp, Decl(assignFromStringInterface2.ts, 13, 10))
>RegExpMatchArray : Symbol(RegExpMatchArray, Decl(lib.es5.d.ts, --, --))

match(regexp: RegExp): string[];
>match : Symbol(NotString.match, Decl(assignFromStringInterface2.ts, 12, 40), Decl(assignFromStringInterface2.ts, 13, 36))
match(regexp: RegExp): RegExpMatchArray;
>match : Symbol(NotString.match, Decl(assignFromStringInterface2.ts, 12, 40), Decl(assignFromStringInterface2.ts, 13, 44))
>regexp : Symbol(regexp, Decl(assignFromStringInterface2.ts, 14, 10))
>RegExp : Symbol(RegExp, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>RegExpMatchArray : Symbol(RegExpMatchArray, Decl(lib.es5.d.ts, --, --))

replace(searchValue: string, replaceValue: string): string;
>replace : Symbol(NotString.replace, Decl(assignFromStringInterface2.ts, 14, 36), Decl(assignFromStringInterface2.ts, 15, 63), Decl(assignFromStringInterface2.ts, 16, 102), Decl(assignFromStringInterface2.ts, 17, 63))
>replace : Symbol(NotString.replace, Decl(assignFromStringInterface2.ts, 14, 44), Decl(assignFromStringInterface2.ts, 15, 63), Decl(assignFromStringInterface2.ts, 16, 102), Decl(assignFromStringInterface2.ts, 17, 63))
>searchValue : Symbol(searchValue, Decl(assignFromStringInterface2.ts, 15, 12))
>replaceValue : Symbol(replaceValue, Decl(assignFromStringInterface2.ts, 15, 32))

replace(searchValue: string, replaceValue: (substring: string, ...args: any[]) => string): string;
>replace : Symbol(NotString.replace, Decl(assignFromStringInterface2.ts, 14, 36), Decl(assignFromStringInterface2.ts, 15, 63), Decl(assignFromStringInterface2.ts, 16, 102), Decl(assignFromStringInterface2.ts, 17, 63))
>replace : Symbol(NotString.replace, Decl(assignFromStringInterface2.ts, 14, 44), Decl(assignFromStringInterface2.ts, 15, 63), Decl(assignFromStringInterface2.ts, 16, 102), Decl(assignFromStringInterface2.ts, 17, 63))
>searchValue : Symbol(searchValue, Decl(assignFromStringInterface2.ts, 16, 12))
>replaceValue : Symbol(replaceValue, Decl(assignFromStringInterface2.ts, 16, 32))
>substring : Symbol(substring, Decl(assignFromStringInterface2.ts, 16, 48))
>args : Symbol(args, Decl(assignFromStringInterface2.ts, 16, 66))

replace(searchValue: RegExp, replaceValue: string): string;
>replace : Symbol(NotString.replace, Decl(assignFromStringInterface2.ts, 14, 36), Decl(assignFromStringInterface2.ts, 15, 63), Decl(assignFromStringInterface2.ts, 16, 102), Decl(assignFromStringInterface2.ts, 17, 63))
>replace : Symbol(NotString.replace, Decl(assignFromStringInterface2.ts, 14, 44), Decl(assignFromStringInterface2.ts, 15, 63), Decl(assignFromStringInterface2.ts, 16, 102), Decl(assignFromStringInterface2.ts, 17, 63))
>searchValue : Symbol(searchValue, Decl(assignFromStringInterface2.ts, 17, 12))
>RegExp : Symbol(RegExp, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>replaceValue : Symbol(replaceValue, Decl(assignFromStringInterface2.ts, 17, 32))

replace(searchValue: RegExp, replaceValue: (substring: string, ...args: any[]) => string): string;
>replace : Symbol(NotString.replace, Decl(assignFromStringInterface2.ts, 14, 36), Decl(assignFromStringInterface2.ts, 15, 63), Decl(assignFromStringInterface2.ts, 16, 102), Decl(assignFromStringInterface2.ts, 17, 63))
>replace : Symbol(NotString.replace, Decl(assignFromStringInterface2.ts, 14, 44), Decl(assignFromStringInterface2.ts, 15, 63), Decl(assignFromStringInterface2.ts, 16, 102), Decl(assignFromStringInterface2.ts, 17, 63))
>searchValue : Symbol(searchValue, Decl(assignFromStringInterface2.ts, 18, 12))
>RegExp : Symbol(RegExp, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>replaceValue : Symbol(replaceValue, Decl(assignFromStringInterface2.ts, 18, 32))
Expand Down Expand Up @@ -147,19 +149,19 @@ var b: NotString;
>b : Symbol(b, Decl(assignFromStringInterface2.ts, 38, 3))
>NotString : Symbol(NotString, Decl(assignFromStringInterface2.ts, 2, 1))

a = x;
a = x;
>a : Symbol(a, Decl(assignFromStringInterface2.ts, 37, 3))
>x : Symbol(x, Decl(assignFromStringInterface2.ts, 36, 3))

a = b;
a = b;
>a : Symbol(a, Decl(assignFromStringInterface2.ts, 37, 3))
>b : Symbol(b, Decl(assignFromStringInterface2.ts, 38, 3))

b = a;
b = a;
>b : Symbol(b, Decl(assignFromStringInterface2.ts, 38, 3))
>a : Symbol(a, Decl(assignFromStringInterface2.ts, 37, 3))

b = x;
b = x;
>b : Symbol(b, Decl(assignFromStringInterface2.ts, 38, 3))
>x : Symbol(x, Decl(assignFromStringInterface2.ts, 36, 3))

Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/assignFromStringInterface2.types
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ interface NotString {
>localeCompare : (that: string) => number
>that : string

match(regexp: string): string[];
>match : { (regexp: string): string[]; (regexp: RegExp): string[]; }
match(regexp: string): RegExpMatchArray;
>match : { (regexp: string): RegExpMatchArray; (regexp: RegExp): RegExpMatchArray; }
>regexp : string

match(regexp: RegExp): string[];
>match : { (regexp: string): string[]; (regexp: RegExp): string[]; }
match(regexp: RegExp): RegExpMatchArray;
>match : { (regexp: string): RegExpMatchArray; (regexp: RegExp): RegExpMatchArray; }
>regexp : RegExp

replace(searchValue: string, replaceValue: string): string;
Expand Down Expand Up @@ -137,22 +137,22 @@ var a: String;
var b: NotString;
>b : NotString

a = x;
a = x;
>a = x : string
>a : String
>x : string

a = b;
a = b;
>a = b : NotString
>a : String
>b : NotString

b = a;
b = a;
>b = a : String
>b : NotString
>a : String

b = x;
b = x;
>b = x : string
>b : NotString
>x : string
Expand Down
18 changes: 6 additions & 12 deletions tests/baselines/reference/bestChoiceType.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
// Repro from #10041

(''.match(/ /) || []).map(s => s.toLowerCase());
>(''.match(/ /) || []).map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>(''.match(/ /) || []).map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>''.match : Symbol(String.match, Decl(lib.es5.d.ts, --, --))
>match : Symbol(String.match, Decl(lib.es5.d.ts, --, --))
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>s : Symbol(s, Decl(bestChoiceType.ts, 2, 26))
>s.toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))
>s : Symbol(s, Decl(bestChoiceType.ts, 2, 26))
>toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))

// Similar cases

Expand All @@ -27,13 +25,11 @@ function f1() {

let z = y.map(s => s.toLowerCase());
>z : Symbol(z, Decl(bestChoiceType.ts, 9, 7))
>y.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>y.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>y : Symbol(y, Decl(bestChoiceType.ts, 8, 7))
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>s : Symbol(s, Decl(bestChoiceType.ts, 9, 18))
>s.toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))
>s : Symbol(s, Decl(bestChoiceType.ts, 9, 18))
>toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))
}

function f2() {
Expand All @@ -51,12 +47,10 @@ function f2() {

let z = y.map(s => s.toLowerCase());
>z : Symbol(z, Decl(bestChoiceType.ts, 15, 7))
>y.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>y.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>y : Symbol(y, Decl(bestChoiceType.ts, 14, 7))
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>s : Symbol(s, Decl(bestChoiceType.ts, 15, 18))
>s.toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))
>s : Symbol(s, Decl(bestChoiceType.ts, 15, 18))
>toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))
}

78 changes: 39 additions & 39 deletions tests/baselines/reference/bestChoiceType.types
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
// Repro from #10041

(''.match(/ /) || []).map(s => s.toLowerCase());
>(''.match(/ /) || []).map(s => s.toLowerCase()) : string[]
>(''.match(/ /) || []).map : <U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]
>(''.match(/ /) || []) : RegExpMatchArray
>''.match(/ /) || [] : RegExpMatchArray
>(''.match(/ /) || []).map(s => s.toLowerCase()) : any[]
>(''.match(/ /) || []).map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
>(''.match(/ /) || []) : RegExpMatchArray | []
>''.match(/ /) || [] : RegExpMatchArray | []
>''.match(/ /) : RegExpMatchArray | null
>''.match : (regexp: string | RegExp) => RegExpMatchArray | null
>'' : ""
>match : (regexp: string | RegExp) => RegExpMatchArray | null
>/ / : RegExp
>[] : never[]
>map : <U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]
>s => s.toLowerCase() : (s: string) => string
>s : string
>s.toLowerCase() : string
>s.toLowerCase : () => string
>s : string
>toLowerCase : () => string
>[] : []
>map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
>s => s.toLowerCase() : (s: any) => any
>s : any
>s.toLowerCase() : any
>s.toLowerCase : any
>s : any
>toLowerCase : any

// Similar cases

Expand All @@ -34,23 +34,23 @@ function f1() {
>/ / : RegExp

let y = x || [];
>y : RegExpMatchArray
>x || [] : RegExpMatchArray
>y : RegExpMatchArray | []
>x || [] : RegExpMatchArray | []
>x : RegExpMatchArray | null
>[] : never[]
>[] : []

let z = y.map(s => s.toLowerCase());
>z : string[]
>y.map(s => s.toLowerCase()) : string[]
>y.map : <U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]
>y : RegExpMatchArray
>map : <U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]
>s => s.toLowerCase() : (s: string) => string
>s : string
>s.toLowerCase() : string
>s.toLowerCase : () => string
>s : string
>toLowerCase : () => string
>z : any[]
>y.map(s => s.toLowerCase()) : any[]
>y.map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
>y : RegExpMatchArray | []
>map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
>s => s.toLowerCase() : (s: any) => any
>s : any
>s.toLowerCase() : any
>s.toLowerCase : any
>s : any
>toLowerCase : any
}

function f2() {
Expand All @@ -65,23 +65,23 @@ function f2() {
>/ / : RegExp

let y = x ? x : [];
>y : RegExpMatchArray
>x ? x : [] : RegExpMatchArray
>y : RegExpMatchArray | never[]
>x ? x : [] : RegExpMatchArray | never[]
>x : RegExpMatchArray | null
>x : RegExpMatchArray
>[] : never[]

let z = y.map(s => s.toLowerCase());
>z : string[]
>y.map(s => s.toLowerCase()) : string[]
>y.map : <U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]
>y : RegExpMatchArray
>map : <U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]
>s => s.toLowerCase() : (s: string) => string
>s : string
>s.toLowerCase() : string
>s.toLowerCase : () => string
>s : string
>toLowerCase : () => string
>z : any[]
>y.map(s => s.toLowerCase()) : any[]
>y.map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
>y : RegExpMatchArray | never[]
>map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
>s => s.toLowerCase() : (s: any) => any
>s : any
>s.toLowerCase() : any
>s.toLowerCase : any
>s : any
>toLowerCase : any
}

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(
a1(...array2); // Error parameter type is (number|string)[]
~~~~~~
!!! error TS2552: Cannot find name 'array2'. Did you mean 'Array'?
!!! related TS2728 /.ts/lib.es5.d.ts:1486:13: 'Array' is declared here.
!!! related TS2728 /.ts/lib.es5.d.ts:1490:13: 'Array' is declared here.
a5([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]]
~~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type '[[any]]'.
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/destructuringTuple.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ tests/cases/compiler/destructuringTuple.ts(11,60): error TS2769: No overload mat
!!! error TS2769: Overload 2 of 3, '(callbackfn: (previousValue: [], currentValue: number, currentIndex: number, array: number[]) => [], initialValue: []): []', gave the following error.
!!! error TS2769: Type 'never[]' is not assignable to type '[]'.
!!! error TS2769: Target allows only 0 element(s) but source may have more.
!!! related TS6502 /.ts/lib.es5.d.ts:1451:24: The expected type comes from the return type of this signature.
!!! related TS6502 /.ts/lib.es5.d.ts:1457:27: The expected type comes from the return type of this signature.
!!! related TS6502 /.ts/lib.es5.d.ts:1455:24: The expected type comes from the return type of this signature.
!!! related TS6502 /.ts/lib.es5.d.ts:1461:27: The expected type comes from the return type of this signature.
~~
!!! error TS2769: No overload matches this call.
!!! error TS2769: Overload 1 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ tests/cases/conformance/types/members/duplicateNumericIndexers.ts(25,5): error T
tests/cases/conformance/types/members/duplicateNumericIndexers.ts(29,5): error TS2374: Duplicate index signature for type 'number'.
tests/cases/conformance/types/members/duplicateNumericIndexers.ts(30,5): error TS2374: Duplicate index signature for type 'number'.
lib.es5.d.ts(517,5): error TS2374: Duplicate index signature for type 'number'.
lib.es5.d.ts(1472,5): error TS2374: Duplicate index signature for type 'number'.
lib.es5.d.ts(1476,5): error TS2374: Duplicate index signature for type 'number'.


==== tests/cases/conformance/types/members/duplicateNumericIndexers.ts (12 errors) ====
Expand Down
15 changes: 15 additions & 0 deletions tests/baselines/reference/firstMatchRegExpMatchArray.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
tests/cases/compiler/firstMatchRegExpMatchArray.ts(5,11): error TS2322: Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.


==== tests/cases/compiler/firstMatchRegExpMatchArray.ts (1 errors) ====
const match = ''.match(/ /)

if (match !== null) {
const foo: string = match[0]
const bar: string = match[1]
~~~
!!! error TS2322: Type 'string | undefined' is not assignable to type 'string'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
}

Loading