Skip to content

Instantly share code, notes, and snippets.

@fujimura
Created August 2, 2017 10:25
Show Gist options
  • Save fujimura/aca7b841fab0f344e76343ba3c1d86a5 to your computer and use it in GitHub Desktop.
Save fujimura/aca7b841fab0f344e76343ba3c1d86a5 to your computer and use it in GitHub Desktop.
// @flow
type A = {
x: number,
y: number,
type: "A"
};
type B = {
x: number,
type: "B"
};
type X = A | B;
const f = (x: X): number => {
if (x.type === "A") {
return x.y;
} else if (x.type === "B") {
return x.x;
} else if (x.type === "C") {
// これは型エラーになって欲しい
return x.z;
} else {
return 0;
}
};
console.log(f({ type: "B", x: 1 }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment