Skip to content

Fix noUncheckedIndexedAccess with tuple rest types and generic index types #40681

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
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
62 changes: 32 additions & 30 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5322,6 +5322,12 @@ namespace ts {
export interface IndexedAccessType extends InstantiableType {
objectType: Type;
indexType: Type;
/**
* @internal
* Indicates that --noUncheckedIndexedAccess may introduce 'undefined' into
* the resulting type, depending on how type variable constraints are resolved.
*/
noUncheckedIndexedAccessCandidate: boolean;
constraint?: Type;
simplifiedForReading?: Type;
simplifiedForWriting?: Type;
Expand Down
35 changes: 34 additions & 1 deletion tests/baselines/reference/noUncheckedIndexedAccess.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@ tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(63,5): error TS2322
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(79,7): error TS2322: Type 'number | boolean | undefined' is not assignable to type 'number | boolean'.
Type 'undefined' is not assignable to type 'number | boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(85,1): error TS2322: Type 'undefined' is not assignable to type 'string'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(90,7): error TS2322: Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(98,5): error TS2322: Type 'undefined' is not assignable to type '{ [key: string]: string; a: string; b: string; }[Key]'.
Type 'undefined' is not assignable to type 'string'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(99,11): error TS2322: Type '{ [key: string]: string; a: string; b: string; }[Key]' is not assignable to type 'string'.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.


==== tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts (28 errors) ====
==== tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts (31 errors) ====
type CheckBooleanOnly<T extends boolean> = any;
// Validate CheckBooleanOnly works - should error
type T_ERR1 = CheckBooleanOnly<boolean | undefined>;
Expand Down Expand Up @@ -216,4 +223,30 @@ tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(85,1): error TS2322
symbolMap[s] = undefined; // Should error
~~~~~~~~~~~~
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.

// Variadic tuples
declare const nonEmptyStringArray: [string, ...string[]];
const variadicOk1: string = nonEmptyStringArray[0]; // Should OK
const variadicError1: string = nonEmptyStringArray[1]; // Should error
~~~~~~~~~~~~~~
!!! error TS2322: Type 'string | undefined' is not assignable to type 'string'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.

// Generic index type
declare const myRecord1: { a: string; b: string };
declare const myRecord2: { a: string; b: string, [key: string]: string };
const fn1 = <Key extends keyof typeof myRecord1>(key: Key): string => myRecord1[key]; // Should OK
const fn2 = <Key extends keyof typeof myRecord1>(key: Key): string => myRecord2[key]; // Should OK
const fn3 = <Key extends keyof typeof myRecord2>(key: Key) => {
myRecord2[key] = undefined; // Should error
~~~~~~~~~~~~~~
!!! error TS2322: Type 'undefined' is not assignable to type '{ [key: string]: string; a: string; b: string; }[Key]'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
const v: string = myRecord2[key]; // Should error
~
!!! error TS2322: Type '{ [key: string]: string; a: string; b: string; }[Key]' is not assignable to type 'string'.
!!! error TS2322: Type 'string | undefined' is not assignable to type 'string'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
};


24 changes: 24 additions & 0 deletions tests/baselines/reference/noUncheckedIndexedAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ declare const s: unique symbol;
declare const symbolMap: { [s]: string };
const e15: string = symbolMap[s]; // Should OK
symbolMap[s] = undefined; // Should error

// Variadic tuples
declare const nonEmptyStringArray: [string, ...string[]];
const variadicOk1: string = nonEmptyStringArray[0]; // Should OK
const variadicError1: string = nonEmptyStringArray[1]; // Should error

// Generic index type
declare const myRecord1: { a: string; b: string };
declare const myRecord2: { a: string; b: string, [key: string]: string };
const fn1 = <Key extends keyof typeof myRecord1>(key: Key): string => myRecord1[key]; // Should OK
const fn2 = <Key extends keyof typeof myRecord1>(key: Key): string => myRecord2[key]; // Should OK
const fn3 = <Key extends keyof typeof myRecord2>(key: Key) => {
myRecord2[key] = undefined; // Should error
const v: string = myRecord2[key]; // Should error
};



//// [noUncheckedIndexedAccess.js]
Expand Down Expand Up @@ -158,3 +174,11 @@ obj1[z];
var f1 = strMapUnion["foo"];
var e15 = symbolMap[s]; // Should OK
symbolMap[s] = undefined; // Should error
var variadicOk1 = nonEmptyStringArray[0]; // Should OK
var variadicError1 = nonEmptyStringArray[1]; // Should error
var fn1 = function (key) { return myRecord1[key]; }; // Should OK
var fn2 = function (key) { return myRecord2[key]; }; // Should OK
var fn3 = function (key) {
myRecord2[key] = undefined; // Should error
var v = myRecord2[key]; // Should error
};
63 changes: 63 additions & 0 deletions tests/baselines/reference/noUncheckedIndexedAccess.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,66 @@ symbolMap[s] = undefined; // Should error
>s : Symbol(s, Decl(noUncheckedIndexedAccess.ts, 81, 13))
>undefined : Symbol(undefined)

// Variadic tuples
declare const nonEmptyStringArray: [string, ...string[]];
>nonEmptyStringArray : Symbol(nonEmptyStringArray, Decl(noUncheckedIndexedAccess.ts, 87, 13))

const variadicOk1: string = nonEmptyStringArray[0]; // Should OK
>variadicOk1 : Symbol(variadicOk1, Decl(noUncheckedIndexedAccess.ts, 88, 5))
>nonEmptyStringArray : Symbol(nonEmptyStringArray, Decl(noUncheckedIndexedAccess.ts, 87, 13))
>0 : Symbol(0)

const variadicError1: string = nonEmptyStringArray[1]; // Should error
>variadicError1 : Symbol(variadicError1, Decl(noUncheckedIndexedAccess.ts, 89, 5))
>nonEmptyStringArray : Symbol(nonEmptyStringArray, Decl(noUncheckedIndexedAccess.ts, 87, 13))

// Generic index type
declare const myRecord1: { a: string; b: string };
>myRecord1 : Symbol(myRecord1, Decl(noUncheckedIndexedAccess.ts, 92, 13))
>a : Symbol(a, Decl(noUncheckedIndexedAccess.ts, 92, 26))
>b : Symbol(b, Decl(noUncheckedIndexedAccess.ts, 92, 37))

declare const myRecord2: { a: string; b: string, [key: string]: string };
>myRecord2 : Symbol(myRecord2, Decl(noUncheckedIndexedAccess.ts, 93, 13))
>a : Symbol(a, Decl(noUncheckedIndexedAccess.ts, 93, 26))
>b : Symbol(b, Decl(noUncheckedIndexedAccess.ts, 93, 37))
>key : Symbol(key, Decl(noUncheckedIndexedAccess.ts, 93, 50))

const fn1 = <Key extends keyof typeof myRecord1>(key: Key): string => myRecord1[key]; // Should OK
>fn1 : Symbol(fn1, Decl(noUncheckedIndexedAccess.ts, 94, 5))
>Key : Symbol(Key, Decl(noUncheckedIndexedAccess.ts, 94, 13))
>myRecord1 : Symbol(myRecord1, Decl(noUncheckedIndexedAccess.ts, 92, 13))
>key : Symbol(key, Decl(noUncheckedIndexedAccess.ts, 94, 49))
>Key : Symbol(Key, Decl(noUncheckedIndexedAccess.ts, 94, 13))
>myRecord1 : Symbol(myRecord1, Decl(noUncheckedIndexedAccess.ts, 92, 13))
>key : Symbol(key, Decl(noUncheckedIndexedAccess.ts, 94, 49))

const fn2 = <Key extends keyof typeof myRecord1>(key: Key): string => myRecord2[key]; // Should OK
>fn2 : Symbol(fn2, Decl(noUncheckedIndexedAccess.ts, 95, 5))
>Key : Symbol(Key, Decl(noUncheckedIndexedAccess.ts, 95, 13))
>myRecord1 : Symbol(myRecord1, Decl(noUncheckedIndexedAccess.ts, 92, 13))
>key : Symbol(key, Decl(noUncheckedIndexedAccess.ts, 95, 49))
>Key : Symbol(Key, Decl(noUncheckedIndexedAccess.ts, 95, 13))
>myRecord2 : Symbol(myRecord2, Decl(noUncheckedIndexedAccess.ts, 93, 13))
>key : Symbol(key, Decl(noUncheckedIndexedAccess.ts, 95, 49))

const fn3 = <Key extends keyof typeof myRecord2>(key: Key) => {
>fn3 : Symbol(fn3, Decl(noUncheckedIndexedAccess.ts, 96, 5))
>Key : Symbol(Key, Decl(noUncheckedIndexedAccess.ts, 96, 13))
>myRecord2 : Symbol(myRecord2, Decl(noUncheckedIndexedAccess.ts, 93, 13))
>key : Symbol(key, Decl(noUncheckedIndexedAccess.ts, 96, 49))
>Key : Symbol(Key, Decl(noUncheckedIndexedAccess.ts, 96, 13))

myRecord2[key] = undefined; // Should error
>myRecord2 : Symbol(myRecord2, Decl(noUncheckedIndexedAccess.ts, 93, 13))
>key : Symbol(key, Decl(noUncheckedIndexedAccess.ts, 96, 49))
>undefined : Symbol(undefined)

const v: string = myRecord2[key]; // Should error
>v : Symbol(v, Decl(noUncheckedIndexedAccess.ts, 98, 9))
>myRecord2 : Symbol(myRecord2, Decl(noUncheckedIndexedAccess.ts, 93, 13))
>key : Symbol(key, Decl(noUncheckedIndexedAccess.ts, 96, 49))

};


68 changes: 68 additions & 0 deletions tests/baselines/reference/noUncheckedIndexedAccess.types
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,71 @@ symbolMap[s] = undefined; // Should error
>s : unique symbol
>undefined : undefined

// Variadic tuples
declare const nonEmptyStringArray: [string, ...string[]];
>nonEmptyStringArray : [string, ...string[]]

const variadicOk1: string = nonEmptyStringArray[0]; // Should OK
>variadicOk1 : string
>nonEmptyStringArray[0] : string
>nonEmptyStringArray : [string, ...string[]]
>0 : 0

const variadicError1: string = nonEmptyStringArray[1]; // Should error
>variadicError1 : string
>nonEmptyStringArray[1] : string | undefined
>nonEmptyStringArray : [string, ...string[]]
>1 : 1

// Generic index type
declare const myRecord1: { a: string; b: string };
>myRecord1 : { a: string; b: string; }
>a : string
>b : string

declare const myRecord2: { a: string; b: string, [key: string]: string };
>myRecord2 : { [key: string]: string; a: string; b: string; }
>a : string
>b : string
>key : string

const fn1 = <Key extends keyof typeof myRecord1>(key: Key): string => myRecord1[key]; // Should OK
>fn1 : <Key extends "a" | "b">(key: Key) => string
><Key extends keyof typeof myRecord1>(key: Key): string => myRecord1[key] : <Key extends "a" | "b">(key: Key) => string
>myRecord1 : { a: string; b: string; }
>key : Key
>myRecord1[key] : { a: string; b: string; }[Key]
>myRecord1 : { a: string; b: string; }
>key : Key

const fn2 = <Key extends keyof typeof myRecord1>(key: Key): string => myRecord2[key]; // Should OK
>fn2 : <Key extends "a" | "b">(key: Key) => string
><Key extends keyof typeof myRecord1>(key: Key): string => myRecord2[key] : <Key extends "a" | "b">(key: Key) => string
>myRecord1 : { a: string; b: string; }
>key : Key
>myRecord2[key] : { [key: string]: string; a: string; b: string; }[Key]
>myRecord2 : { [key: string]: string; a: string; b: string; }
>key : Key

const fn3 = <Key extends keyof typeof myRecord2>(key: Key) => {
>fn3 : <Key extends string | number>(key: Key) => void
><Key extends keyof typeof myRecord2>(key: Key) => { myRecord2[key] = undefined; // Should error const v: string = myRecord2[key]; // Should error} : <Key extends string | number>(key: Key) => void
>myRecord2 : { [key: string]: string; a: string; b: string; }
>key : Key

myRecord2[key] = undefined; // Should error
>myRecord2[key] = undefined : undefined
>myRecord2[key] : { [key: string]: string; a: string; b: string; }[Key]
>myRecord2 : { [key: string]: string; a: string; b: string; }
>key : Key
>undefined : undefined

const v: string = myRecord2[key]; // Should error
>v : string
>myRecord2[key] : { [key: string]: string; a: string; b: string; }[Key]
>myRecord2 : { [key: string]: string; a: string; b: string; }
>key : Key

};


16 changes: 16 additions & 0 deletions tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,19 @@ declare const s: unique symbol;
declare const symbolMap: { [s]: string };
const e15: string = symbolMap[s]; // Should OK
symbolMap[s] = undefined; // Should error

// Variadic tuples
declare const nonEmptyStringArray: [string, ...string[]];
const variadicOk1: string = nonEmptyStringArray[0]; // Should OK
const variadicError1: string = nonEmptyStringArray[1]; // Should error

// Generic index type
declare const myRecord1: { a: string; b: string };
declare const myRecord2: { a: string; b: string, [key: string]: string };
const fn1 = <Key extends keyof typeof myRecord1>(key: Key): string => myRecord1[key]; // Should OK
const fn2 = <Key extends keyof typeof myRecord1>(key: Key): string => myRecord2[key]; // Should OK
const fn3 = <Key extends keyof typeof myRecord2>(key: Key) => {
myRecord2[key] = undefined; // Should error
const v: string = myRecord2[key]; // Should error
};