TypeScript/tests/cases/compiler/discriminantElementAccessCheck.ts
Andrii Dieiev a34fdb203e Better template literals support in checker (#32064)
* Support template literals in enum declarations

* Support template literals in const enum access

* Support template literals in swith with typeof narrowing

* Support template literals in element access discriminant

* Support template literals in ambient module declaration

* Unify symbols for template literals in computed properties

* Unify expression position checks for template literals

* Support template literals in rename and find all references

* Mark computed properties with template literals as write access

* Inline startsWithQuote
2019-09-27 12:04:13 -07:00

52 lines
915 B
TypeScript

type U = TypeA | TypeB;
interface TypeA {
kind: 'A';
a: number;
}
interface TypeB {
kind: 'B';
b: string;
}
function assertNever(x: never) {
return x;
}
function IfWithString(val: U) {
if (val['kind'] === 'B') {
return val.b;
} else {
return val.a;
}
}
function SwitchWithString(val: U) {
switch (val['kind']) {
case 'A':
return val.a;
case 'B':
return val.b;
default:
return assertNever(val);
}
}
function IfWithTemplate(val: U) {
if (val[`kind`] === 'B') {
return val.b;
} else {
return val.a;
}
}
function SwitchWithTemplate(val: U) {
switch (val[`kind`]) {
case 'A':
return val.a;
case 'B':
return val.b;
default:
return assertNever(val);
}
}