1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(3,32): error TS2344: Type 'boolean | undefined' does not satisfy the constraint 'boolean'.
Type 'undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(12,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
Type 'undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(13,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(14,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(15,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(16,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(17,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(18,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(19,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(20,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(21,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(22,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(23,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(24,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(25,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(38,1): error TS2322: Type 'undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(39,1): error TS2322: Type 'undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(40,1): error TS2322: Type 'undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(41,1): error TS2322: Type 'undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(46,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(47,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(48,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(49,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(50,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(55,5): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
tests/cases/conformance/pedantic/noUncheckedIndexedAccess.ts(63,5): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
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 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
==== 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>;
~~~~~~~~~~~~~~~~~~~
!!! error TS2344: Type 'boolean | undefined' does not satisfy the constraint 'boolean'.
!!! error TS2344: Type 'undefined' is not assignable to type 'boolean'.
enum NumericEnum1 { A, B, C }
enum NumericEnum2 { A = 0, B = 1 , C = 2 }
enum StringEnum1 { A = "Alpha", B = "Beta" }
declare const strMap: { [s: string]: boolean };
// All of these should be errors
const e1: boolean = strMap["foo"];
~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
!!! error TS2322: Type 'undefined' is not assignable to type 'boolean'.
const e2: boolean = strMap.bar;
~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
const e3: boolean = strMap[0];
~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
const e4: boolean = strMap[0 as string | number];
~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
const e5: boolean = strMap[0 as string | 0 | 1];
~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
const e6: boolean = strMap[0 as 0 | 1];
~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
const e7: boolean = strMap["foo" as "foo" | "baz"];
~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
const e8: boolean = strMap[NumericEnum1.A];
~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
const e9: boolean = strMap[NumericEnum2.A];
~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
const e10: boolean = strMap[StringEnum1.A];
~~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
const e11: boolean = strMap[StringEnum1.A as StringEnum1];
~~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
const e12: boolean = strMap[NumericEnum1.A as NumericEnum1];
~~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
const e13: boolean = strMap[NumericEnum2.A as NumericEnum2];
~~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
const e14: boolean = strMap[null as any];
~~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
// Should be OK
const ok1: boolean | undefined = strMap["foo"];
const ok2: boolean | undefined = strMap.bar;
type T_OK1 = CheckBooleanOnly<(typeof strMap)[string]>;
type T_OK2 = CheckBooleanOnly<(typeof strMap)["foo"]>;
type T_OK3 = CheckBooleanOnly<(typeof strMap)["bar" | "baz"]>;
type T_OK4 = CheckBooleanOnly<(typeof strMap)[number]>;
type T_OK5 = CheckBooleanOnly<(typeof strMap)[any]>;
// Writes don't allow 'undefined'; all should be errors
strMap["baz"] = undefined;
~~~~~~~~~~~~~
!!! error TS2322: Type 'undefined' is not assignable to type 'boolean'.
strMap.qua = undefined;
~~~~~~~~~~
!!! error TS2322: Type 'undefined' is not assignable to type 'boolean'.
strMap[0] = undefined;
~~~~~~~~~
!!! error TS2322: Type 'undefined' is not assignable to type 'boolean'.
strMap[null as any] = undefined;
~~~~~~~~~~~~~~~~~~~
!!! error TS2322: Type 'undefined' is not assignable to type 'boolean'.
// Numeric lookups are unaffected
declare const numMap: { [s: number]: boolean };
// All of these should be ok
const num_ok1: boolean = numMap[0];
~~~~~~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
const num_ok2: boolean = numMap[0 as number];
~~~~~~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
const num_ok3: boolean = numMap[0 as 0 | 1];
~~~~~~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
const num_ok4: boolean = numMap[NumericEnum1.A];
~~~~~~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
const num_ok5: boolean = numMap[NumericEnum2.A];
~~~~~~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
// Generics
function generic1<T extends { [s: string]: boolean }>(arg: T): boolean {
// Should error
return arg["blah"];
~~~~~~~~~~~~~~~~~~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
}
function generic2<T extends { [s: string]: boolean }>(arg: T): boolean {
// Should OK
return arg["blah"]!;
}
function generic3<T extends string>(arg: T): boolean {
// Should error
return strMap[arg];
~~~~~~~~~~~~~~~~~~~
!!! error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
}
// Element access into known properties is ok
declare const obj1: { x: string, y: number, [key: string]: string | number };
obj1["x"];
const y = "y";
obj1[y];
let yy = "y";
obj1[yy];
let z = "z";
obj1[z];
// Distributivity cases
declare const strMapUnion: { [s: string]: boolean } | { [s: string]: number };
// Should error
const f1: boolean | number = strMapUnion["foo"];
~~
!!! error TS2322: Type 'number | boolean | undefined' is not assignable to type 'number | boolean'.
!!! error TS2322: Type 'undefined' is not assignable to type 'number | boolean'.
// Symbol index signatures
declare const s: unique symbol;
declare const symbolMap: { [s]: string };
const e15: string = symbolMap[s]; // Should OK
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 'string | undefined' is not assignable to type 'string'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
};
|