Skip to content

Instantly share code, notes, and snippets.

@syrnick
Created September 19, 2016 17:28
Show Gist options
  • Save syrnick/f17c1d98d7b7f998b0f82adf4dc1818c to your computer and use it in GitHub Desktop.
Save syrnick/f17c1d98d7b7f998b0f82adf4dc1818c to your computer and use it in GitHub Desktop.
// @flow
declare function SomeFn(): void;
type A = {
type: 'simple';
foo: number;
requiredName: string;
};
type B = {
type: 'error';
bar: number;
errorHandler: SomeFn;
};
type AorB = A | B;
class MyClass {
_states: {[key: string]: AorB};
constructor() {
this._states = {};
}
addB(n: string): void {
this._states[n] = {
type: 'error',
foo: 1,
};
}
}
@kasrak
Copy link

kasrak commented Sep 19, 2016

fwiw this catches the mistake, not sure what's going on in your example :/

type A = {
    type: 'simple';
    foo: number;
    requiredName: string;
};

type B = {
    type: 'error';
    bar: number;
};

type AorB = A | B;

let foo: A = {type: 'simple', foo: 2, requiredName: 'asdf'};
let bar: B = {type: 'error', bar: 3};

let obj: {[key: string]: AorB} = {};
obj['a'] = foo;
obj['b'] = bar;
obj['c'] = {type: 'error', foo: 1}; // This type is incompatible with union: A | B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment