Skip to content

Commit 04b3256

Browse files
Fix crash for destructuring under noUncheckedIndexedAccess + noLib. (#3760)
1 parent 968d062 commit 04b3256

6 files changed

Lines changed: 131 additions & 0 deletions

internal/checker/flow.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,6 +2314,9 @@ func (c *Checker) getTypeOfDestructuredArrayElement(t *Type, index int) *Type {
23142314
}
23152315

23162316
func (c *Checker) includeUndefinedInIndexSignature(t *Type) *Type {
2317+
if t == nil {
2318+
return nil
2319+
}
23172320
if c.compilerOptions.NoUncheckedIndexedAccess == core.TSTrue {
23182321
return c.getUnionType([]*Type{t, c.missingType})
23192322
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
input.ts(2,6): error TS2339: Property '0' does not exist on type 'string[]'.
2+
3+
4+
==== globals.ts (0 errors) ====
5+
interface Array<T> {}
6+
interface Boolean {}
7+
interface Function {}
8+
interface CallableFunction {}
9+
interface NewableFunction {}
10+
interface IArguments {}
11+
interface Number {}
12+
interface Object {}
13+
interface RegExp {}
14+
interface String {}
15+
16+
==== input.ts (1 errors) ====
17+
declare var x: string[];
18+
var [a] = x;
19+
~
20+
!!! error TS2339: Property '0' does not exist on type 'string[]'.
21+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [tests/cases/compiler/noLibAndNoUncheckedIndexedAccessDestructuringArray.ts] ////
2+
3+
//// [globals.ts]
4+
interface Array<T> {}
5+
interface Boolean {}
6+
interface Function {}
7+
interface CallableFunction {}
8+
interface NewableFunction {}
9+
interface IArguments {}
10+
interface Number {}
11+
interface Object {}
12+
interface RegExp {}
13+
interface String {}
14+
15+
//// [input.ts]
16+
declare var x: string[];
17+
var [a] = x;
18+
19+
20+
//// [globals.js]
21+
"use strict";
22+
//// [input.js]
23+
"use strict";
24+
var [a] = x;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//// [tests/cases/compiler/noLibAndNoUncheckedIndexedAccessDestructuringArray.ts] ////
2+
3+
=== globals.ts ===
4+
interface Array<T> {}
5+
>Array : Symbol(Array, Decl(globals.ts, 0, 0))
6+
>T : Symbol(T, Decl(globals.ts, 0, 16))
7+
8+
interface Boolean {}
9+
>Boolean : Symbol(Boolean, Decl(globals.ts, 0, 21))
10+
11+
interface Function {}
12+
>Function : Symbol(Function, Decl(globals.ts, 1, 20))
13+
14+
interface CallableFunction {}
15+
>CallableFunction : Symbol(CallableFunction, Decl(globals.ts, 2, 21))
16+
17+
interface NewableFunction {}
18+
>NewableFunction : Symbol(NewableFunction, Decl(globals.ts, 3, 29))
19+
20+
interface IArguments {}
21+
>IArguments : Symbol(IArguments, Decl(globals.ts, 4, 28))
22+
23+
interface Number {}
24+
>Number : Symbol(Number, Decl(globals.ts, 5, 23))
25+
26+
interface Object {}
27+
>Object : Symbol(Object, Decl(globals.ts, 6, 19))
28+
29+
interface RegExp {}
30+
>RegExp : Symbol(RegExp, Decl(globals.ts, 7, 19))
31+
32+
interface String {}
33+
>String : Symbol(String, Decl(globals.ts, 8, 19))
34+
35+
=== input.ts ===
36+
declare var x: string[];
37+
>x : Symbol(x, Decl(input.ts, 0, 11))
38+
39+
var [a] = x;
40+
>a : Symbol(a, Decl(input.ts, 1, 5))
41+
>x : Symbol(x, Decl(input.ts, 0, 11))
42+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [tests/cases/compiler/noLibAndNoUncheckedIndexedAccessDestructuringArray.ts] ////
2+
3+
=== globals.ts ===
4+
5+
interface Array<T> {}
6+
interface Boolean {}
7+
interface Function {}
8+
interface CallableFunction {}
9+
interface NewableFunction {}
10+
interface IArguments {}
11+
interface Number {}
12+
interface Object {}
13+
interface RegExp {}
14+
interface String {}
15+
16+
=== input.ts ===
17+
declare var x: string[];
18+
>x : string[]
19+
20+
var [a] = x;
21+
>a : any
22+
>x : string[]
23+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @noUncheckedIndexedAccess: true
2+
// @noLib: true
3+
4+
// @filename: globals.ts
5+
interface Array<T> {}
6+
interface Boolean {}
7+
interface Function {}
8+
interface CallableFunction {}
9+
interface NewableFunction {}
10+
interface IArguments {}
11+
interface Number {}
12+
interface Object {}
13+
interface RegExp {}
14+
interface String {}
15+
16+
// @filename: input.ts
17+
declare var x: string[];
18+
var [a] = x;

0 commit comments

Comments
 (0)