Skip to content

Instantly share code, notes, and snippets.

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