TypeScript/tests/cases/compiler/switchCaseNarrowsMatchingClausesEvenWhenNonMatchingClausesExist.ts
Wesley Wigham 8c5ad2429a
Relax switch-case narrowing restrictions (#23522)
* Allow switch case narrowing even when individual clauses are nonunit

* And remove unit type restriction

* Rename
2018-04-27 16:30:43 -07:00

40 lines
916 B
TypeScript

export const narrowToLiterals = (str: string) => {
switch (str) {
case 'abc': {
// inferred type as `abc`
return str;
}
default:
return 'defaultValue';
}
};
export const narrowToString = (str: string, someOtherStr: string) => {
switch (str) {
case 'abc': {
// inferred type should be `abc`
return str;
}
case someOtherStr: {
// `string`
return str;
}
default:
return 'defaultValue';
}
};
export const narrowToStringOrNumber = (str: string | number, someNumber: number) => {
switch (str) {
case 'abc': {
// inferred type should be `abc`
return str;
}
case someNumber: {
// inferred type should be `number`
return str;
}
default:
return 'defaultValue';
}
};