Skip to content

Instantly share code, notes, and snippets.

@blemoine
Last active March 31, 2018 20:36
Show Gist options
  • Save blemoine/48390ff087d09f9d5a41bf4640154f45 to your computer and use it in GitHub Desktop.
Save blemoine/48390ff087d09f9d5a41bf4640154f45 to your computer and use it in GitHub Desktop.
const arr: Array<string | number> = [1, 'a', 2];
arr.forEach((value: string | number) => {
if (typeof value === 'number') {
//Here `value` is known to be a number by the compiler
console.log(value * 2);
} else {
//Here `value` is known to be a string by the compiler
console.log(value.substr(1));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment