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

WIP: Member-type based type guards #6062

Closed
wants to merge 7 commits into from
Closed
Changes from 1 commit
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
Next Next commit
initial pass at equality as member type guards
  • Loading branch information
weswigham committed Apr 26, 2016
commit b3db6ff16fe9e395d4e573f32e88216fc21a27c2
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
interface Discriminator {
_discriminator: void;
}

interface FooDiscriminator extends Discriminator {
_foo: void;
}

interface BarDiscriminator extends Discriminator {
_bar: void;
}

interface BaseNode {
kind: Discriminator;
}

interface FooNode extends BaseNode {
kind: FooDiscriminator;
foo: string;
}

interface BarNode extends BaseNode {
kind: BarDiscriminator;
bar: string;
}

let a: FooDiscriminator;
let x: FooNode | BarNode;

if (x.kind === a) {
x.foo = "yay!";
}
else {
x; // Not narrowed at present
}

let z: {
value: string;
item: FooNode | BarNode;
}
if (z.item.kind === a) {
z.item.foo = "cool!";
z.value = "yes";
}