Skip to content

Instantly share code, notes, and snippets.

@tkssharma
Created October 28, 2018 02:00
Show Gist options
  • Save tkssharma/014ed79866b5f5150faa8c7b674225ef to your computer and use it in GitHub Desktop.
Save tkssharma/014ed79866b5f5150faa8c7b674225ef to your computer and use it in GitHub Desktop.
function returnNever(i: number): never {
// Logic here
if (i === 0) {
throw Error("i is zero");
} else {
throw Error("i is not zero");
}
// Will never reach the end of the function
}
//-----------------------------------------//
type myUnion = "a" | "b";
function elseNever(value: myUnion) {
if (value === "a") {
value; // type is “a”
} else if (value === "b") {
value; // type is “b”
} else {
value; // type is never
}
}
// enforcing types in a List
let arrayWithSquareBrackets: number[] = [1, 2, 3];
let arrayWithObject: Array<number> = [1, 2, 3];
let arrayWithObjectNew: Array<number> = new Array<number>(1, 2, 3);
let arrayWithSquareBrackets2: (number | string)[] = [1, 2, "one", "two"];
let arrayWithObject2: Array<number | string> = [1, 2, "one", "two"];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment