Skip to content

Instantly share code, notes, and snippets.

@jamiebuilds
Created August 1, 2016 22:27
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiebuilds/24a4e32be4899419e53eec655e538a7f to your computer and use it in GitHub Desktop.
Save jamiebuilds/24a4e32be4899419e53eec655e538a7f to your computer and use it in GitHub Desktop.

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