Created
February 28, 2018 14:12
-
-
Save floydawong/f8b5cb8c9cf4f59ec3657f3e48edd907 to your computer and use it in GitHub Desktop.
TS_BasicTypes
This file contains 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
// Basic Types | |
// Boolean | |
let isDone: boolean = false | |
// Number | |
let decimal: number = 6 | |
let hex: number = 0xf00d | |
let binary: number = 0b1010 | |
let octal: number = 0o744 | |
// String | |
let color: string = "blue" | |
color = 'red' | |
let fullName: string = `Bob Bobbington` | |
let age: number = 37 | |
var sentence: string = `Hello, my name is ${fullName}. | |
I'll be ${ age + 1} years old next month.` | |
var sentence: string = "Hello, my name is " + fullName + ".\n\n" + | |
"I'll be " + (age + 1) + " years old next month." | |
// Array | |
var list: number[] = [1, 2, 3] | |
var list: Array<number> = [1, 2, 3] | |
// var list: Array<string> = ["1", "2", "3"] // Variable 'list' must be of type 'number[]', but here has type 'string[]'. | |
console.log(list) | |
// Tuple | |
// Declare a tuple type | |
let x: [string, number] | |
// Initialize it | |
x = ["hello", 10] // OK | |
// Initialize it incorrectly | |
// x = [10, "hello"] // Error | |
console.log(x[0].substr(1)) // OK | |
// console.log(x[1].substr(1)) // Error, 'number' does not have 'substr' | |
x[3] = "world" // OK, 'string' can be assigned to 'string | number' | |
console.log(x[1].toString()) // OK, 'string' and 'number' both have 'toString' | |
// x[1] = true // Error, 'boolean' isn't 'string | number' | |
// Enum | |
enum Color1 { Red = 10, Green, Blue } | |
var c: Color1 = Color1.Green | |
console.log(c) // 11 | |
enum Color2 { Red = 1, Green = 5, Blue = 4 } | |
var cc: Color2 = Color2.Green | |
console.log(cc) // 5 | |
enum Color3 { Red = 1, Green, Blue } | |
let colorName: string = Color3[2] | |
console.log(colorName) // Displays 'Green' as its value is 2 above | |
// Any | |
var notSure: any = 4 | |
notSure = "maybe a string instead" | |
notSure = false // okay, definitely a boolean | |
var notSure: any = 4 | |
notSure.toFixed() // okay, toFixed exists (but the compiler doesn't check) | |
let prettySure: Object = 4 | |
// prettySure.toFixed() // Error: Property 'toFixed' doesn't exist on type 'Object'. | |
let list_any: any[] = [1, true, "free"] | |
list_any[1] = 100 | |
console.log(list_any) // [ 1, 100, 'free' ] | |
// Void | |
function warnUser(): void { | |
alert("This is my warning message") | |
} | |
let unusable: void = undefined; | |
// Null and Undefined | |
// Not much else we can assign to these variables! | |
let u: undefined = undefined; | |
let n: null = null; | |
// Never | |
// Function returning never must have unreachable end point | |
function error(message: string): never { | |
throw new Error(message); | |
} | |
// Inferred return type is never | |
function fail() { | |
return error("Something failed"); | |
} | |
// Function returning never must have unreachable end point | |
function infiniteLoop(): never { | |
while (true) { | |
} | |
} | |
// Type assertions | |
var someValue: any = "this is a string"; | |
var strLength: number = (<string>someValue).length; | |
var someValue: any = "this is a string"; | |
var strLength: number = (someValue as string).length; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment