Skip to content
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

Fix #29710: Narrow unknown in switch #29711

Merged
merged 3 commits into from
Feb 5, 2019
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
26 changes: 25 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16152,13 +16152,37 @@ namespace ts {
}

function narrowTypeBySwitchOnDiscriminant(type: Type, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number) {
// We only narrow if all case expressions specify values with unit types
// We only narrow if all case expressions specify
// values with unit types, except for the case where
// `type` is unknown. In this instance we map object
// types to the nonPrimitive type and narrow with that.
const switchTypes = getSwitchClauseTypes(switchStatement);
if (!switchTypes.length) {
return type;
}
const clauseTypes = switchTypes.slice(clauseStart, clauseEnd);
const hasDefaultClause = clauseStart === clauseEnd || contains(clauseTypes, neverType);
if ((type.flags & TypeFlags.Unknown) && !hasDefaultClause) {
let groundClauseTypes: Type[] | undefined;
for (let i = 0; i < clauseTypes.length; i += 1) {
const t = clauseTypes[i];
if (t.flags & (TypeFlags.Primitive | TypeFlags.NonPrimitive)) {
if (groundClauseTypes !== undefined) {
groundClauseTypes.push(t);
}
}
else if (t.flags & TypeFlags.Object) {
if (groundClauseTypes === undefined) {
groundClauseTypes = clauseTypes.slice(0, i);
}
groundClauseTypes.push(nonPrimitiveType);
}
else {
return type;
}
}
return getUnionType(groundClauseTypes === undefined ? clauseTypes : groundClauseTypes);
}
const discriminantType = getUnionType(clauseTypes);
const caseType =
discriminantType.flags & TypeFlags.Never ? neverType :
Expand Down
231 changes: 231 additions & 0 deletions tests/baselines/reference/unknownType2.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
tests/cases/conformance/types/unknown/unknownType2.ts(216,13): error TS2322: Type '"yes" | "no" | "maybe"' is not assignable to type 'SomeResponse'.
Type '"maybe"' is not assignable to type 'SomeResponse'.


==== tests/cases/conformance/types/unknown/unknownType2.ts (1 errors) ====
type isUnknown<T> = unknown extends T ? true : false;
type isTrue<T extends true> = T;

type SomeResponse = 'yes' | 'no' | 'idk';
let validate: (x: unknown) => SomeResponse = x => (x === 'yes' || x === 'no') ? x : 'idk'; // No error

const u: unknown = undefined;

declare const symb: unique symbol;
declare const symbNonUnique: symbol;

if (u === 5) {
const y = u.toString(10);
}

if (u === true || u === false) {
const someBool: boolean = u;
}

if (u === undefined) {
const undef: undefined = u;
}

if (u === null) {
const someNull: null = u;
}

if (u === symb) {
const symbolAlias: typeof symb = u;
}

if (!(u === 42)) {
type A = isTrue<isUnknown<typeof u>>
}

if (u !== 42) {
type B = isTrue<isUnknown<typeof u>>
}

if (u == 42) {
type C = isTrue<isUnknown<typeof u>>
}

if (u == true) {
type D = isTrue<isUnknown<typeof u>>
}

if (u == Object) {
type E = isTrue<isUnknown<typeof u>>
}

declare const aString: string;
declare const aBoolean: boolean;
declare const aNumber: number;
declare const anObject: object;
declare const anObjectLiteral: { x: number };
declare const aUnion: { x: number } | { y: string };
declare const anIntersection: { x: number } & { y: string };
declare const aFunction: () => number;

if (u === aString) {
let uString: string = u;
}

if (u === aBoolean) {
let uString: boolean = u;
}

if (u === aNumber) {
let uNumber: number = u;
}

if (u === anObject) {
let uObject: object = u;
}

if (u === anObjectLiteral) {
let uObjectLiteral: object = u;
}

if (u === aUnion) {
type unionDoesNotNarrow = isTrue<isUnknown<typeof u>>
}

if (u === anIntersection) {
type intersectionDoesNotNarrow = isTrue<isUnknown<typeof u>>
}

if (u === aFunction) {
let uFunction: object = u;
}

enum NumberEnum {
A,
B,
C
}

enum StringEnum {
A = "A",
B = "B",
C = "C"
}

if (u === NumberEnum || u === StringEnum) {
let enumObj: object = u;
}

if (u === NumberEnum.A) {
let a: NumberEnum.A = u
}

if (u === StringEnum.B) {
let b: StringEnum.B = u
}

function switchTestEnum(x: unknown) {
switch (x) {
case StringEnum.A:
const a: StringEnum.A = x;
break;
case StringEnum.B:
const b: StringEnum.B = x;
break;
case StringEnum.C:
const c: StringEnum.C = x;
break;
}
type End = isTrue<isUnknown<typeof x>>
}

function switchTestCollectEnum(x: unknown) {
switch (x) {
case StringEnum.A:
const a: StringEnum.A = x;
case StringEnum.B:
const b: StringEnum.A | StringEnum.B = x;
case StringEnum.C:
const c: StringEnum.A | StringEnum.B | StringEnum.C = x;
const all: StringEnum = x;
return;
}
type End = isTrue<isUnknown<typeof x>>
}

function switchTestLiterals(x: unknown) {
switch (x) {
case 1:
const one: 1 = x;
break;
case 2:
const two: 2 = x;
break;
case 3:
const three: 3 = x;
break;
case true:
const t: true = x;
break;
case false:
const f: false = x;
break;
case "A":
const a: "A" = x;
break;
case undefined:
const undef: undefined = x;
break;
case null:
const llun: null = x;
break;
case symb:
const anotherSymbol: typeof symb = x;
break;
case symbNonUnique:
const nonUniqueSymbol: symbol = x;
break;
}
type End = isTrue<isUnknown<typeof x>>
}

function switchTestObjects(x: unknown, y: () => void, z: { prop: number }) {
switch (x) {
case true:
case false:
const bool: boolean = x;
break;
case y:
const obj1: object = x;
break;
case z:
const obj2: object = x;
break;
}
type End = isTrue<isUnknown<typeof x>>
}

function switchResponse(x: unknown): SomeResponse {
switch (x) {
case 'yes':
case 'no':
case 'idk':
return x;
default:
throw new Error('unknown response');
}
// Arguably this should be never.
type End = isTrue<isUnknown<typeof x>>
}

function switchResponseWrong(x: unknown): SomeResponse {
switch (x) {
case 'yes':
case 'no':
case 'maybe':
return x; // error
~~~~~~~~~
!!! error TS2322: Type '"yes" | "no" | "maybe"' is not assignable to type 'SomeResponse'.
!!! error TS2322: Type '"maybe"' is not assignable to type 'SomeResponse'.
default:
throw new Error('Can you repeat the question?');
}
// Arguably this should be never.
type End = isTrue<isUnknown<typeof x>>
}

Loading