Skip to content

Commit

Permalink
Added more (failing) test cases for in typeguard type widening
Browse files Browse the repository at this point in the history
  • Loading branch information
marekdedic committed Oct 19, 2021
1 parent 3e70f10 commit 5def93a
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/cases/conformance/controlFlow/controlFlowInOperator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const a = 'a';
const b = 'b';
const d = 'd';

// Type narrowing

type A = { [a]: number; };
type B = { [b]: string; };

Expand Down Expand Up @@ -34,3 +36,36 @@ if (d in c) {
} else {
c; // (A | B)
}

// Type widening

declare const e: unknown;
declare const f: any;

if ('a' in e) {
e; // { a: unknown; }
e['a'] // unknown
} else {
e; // unknown
}

if ('b' in f) {
f; // { b: unknown; }
f['b'] // unknown
} else {
f; // any
}

if (a in e) {
e; // { a: unknown; }
e[a] // unknown
} else {
e; // unknown
}

if (b in f) {
f; // { b: unknown; }
f[b] // unknown
} else {
f; // any
}

0 comments on commit 5def93a

Please sign in to comment.