Created
October 28, 2018 02:00
-
-
Save tkssharma/014ed79866b5f5150faa8c7b674225ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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