Skip to content

Instantly share code, notes, and snippets.

@jeffmo
Created April 18, 2016 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffmo/b28cf7eb21dc783e6026a5617fe88075 to your computer and use it in GitHub Desktop.
Save jeffmo/b28cf7eb21dc783e6026a5617fe88075 to your computer and use it in GitHub Desktop.
// @flow
declare function getElem(name: 'a'): HTMLCollection<HTMLAnchorElement>;
declare function getElem(name: 'button'): HTMLCollection<HTMLButtonElement>;
function getElem(name) {
if (name === 'a') {
return new HTMLCollection(/*...*/);
} else {
return new HTMLCollection(/*...*/);
}
};
let a: HTMLCollection<HTMLButtonElement> = getElem('a'); // Error
let b: HTMLCollection<HTMLAnchorElement> = getElem('a'); // No Error
> flow version
Flow, a static type checker for JavaScript, version 0.23.0
> flow check
main.js:4
4: declare function getElem(name: 'a'): HTMLCollection<HTMLAnchorElement>;
^^^^^^^^^^^^^^^^^ HTMLAnchorElement. This type is incompatible with
15: let a: HTMLCollection<HTMLButtonElement> = getElem('a'); // Error
^^^^^^^^^^^^^^^^^ HTMLButtonElement
main.js:15
15: let a: HTMLCollection<HTMLButtonElement> = getElem('a'); // Error
^^^^^^^^^^^^^^^^^ HTMLButtonElement. This type is incompatible with
4: declare function getElem(name: 'a'): HTMLCollection<HTMLAnchorElement>;
^^^^^^^^^^^^^^^^^ HTMLAnchorElement
Found 2 errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment