Skip to content

Instantly share code, notes, and snippets.

@jamiebuilds
Created August 1, 2016 22:27
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?

Input

function example(obj: { prop: string | number }): string {
  if (typeof obj.prop === 'string') {
    mutatesProp(obj);
    return obj.prop;
  } else {
    return "default";
  }
}

function mutatesProp(obj) {
  obj.prop = 42;
}

example({ prop: 'test' });

TypeScript Output

No errors

This is what you get even with the --strictNullChecks flag

Flow Output

6:     return obj.prop;
              ^ number. This type is incompatible with the expected return type of
3: function example(obj: { prop: string | number }): string {
                                                     ^ string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment