Created
November 18, 2018 14:41
-
-
Save sangheestyle/9b742b4b08c38a0d38b3bdc9011cfd3f to your computer and use it in GitHub Desktop.
Reading TypeScript handbook from https://www.typescriptlang.org/docs/handbook/basic-types.html
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
// TS basic types | |
// from https://www.typescriptlang.org/docs/handbook/basic-types.html | |
// Boolean | |
let isDone: boolean = false; | |
// Number (floating point values like JS) | |
let decimal: number = 6; | |
let float: number = 6.1; | |
let hex: number = 0xf00d; | |
let binary: number = 0b1010; | |
let octal: number = 0o744; | |
// String | |
let color: string = "blue"; | |
// A generic arry type | |
let list: Array<number> = [1, 2, 3] | |
// Tuple | |
let x: [string, number]; | |
x = ["hello", 10]; | |
console.log(x[0].substr(1, 3)); | |
// Enum | |
enum Color {Red, Green, Blue} | |
let c: Color = Color.Green; | |
// Any | |
let notSure: any = 4; | |
notSure = "hi"; | |
notSure = false; | |
let anotherList: Array<any> = [1, true, "free"]; | |
let secondList: any[] = [2, false, "paid"]; | |
// Void: the opposite of any | |
// the absence of having any type at all | |
function warnUser(): void { | |
console.log("This is my warning message"); | |
} | |
// Null and Undefined | |
// useful with --strictNullChecks flag | |
let u: undefined = undefined; | |
let n: null = null; | |
// Never: the tpye of values that never occur | |
// 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"); | |
} | |
// Object: a type that represents the non-primitive type. | |
// anything that is not number, string, boolean, | |
// symbol, null, or undefined. | |
declare function create(o: object | null): void; | |
create({ prop: 0 }); | |
create(null); | |
create(42); // Error | |
create("string"); // Error | |
create(false); // Error | |
create(undefined); // Error | |
// Type assertions: like a type cast in other languages | |
// * no runtime impact | |
// * used purely by the compiler | |
// angle-bracket syntax | |
let someValue: any = "this is a string"; | |
let strLength: number = (<string>someValue).length; | |
// the as-syntax (JSX only allows as) | |
let anotherStrLength: number = (someValue as string).length; |
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
// let and const are preferable to var | |
// why? | |
// 1. variable capturing quirks | |
// a. refers to the same i from the same scope. | |
for (var i = 0; i < 10; i++) { | |
setTimeout(function() { console.log(i); }, 100 * i); | |
} | |
// b. Re-declarations and Shadowing | |
function f(x) { | |
var x; | |
var x; | |
if (true) { | |
var x; | |
} | |
} | |
// var: function scoping | |
// let: block scoping | |
// const: immutable | |
// let vs const | |
// * Applying the principle of least privilege: starts with const | |
// Destructuring from ES6(ECMAScript 2015) | |
// a. Array destructuring | |
let input = [1, 2]; | |
let [first, second] = input; | |
// swap variables | |
[first, second] = [second, first]; | |
// remaining items | |
let [first, ...rest] = [1, 2, 3, 4]; | |
console.log(first); // outputs 1 | |
console.log(rest); // outputs [ 2, 3, 4 ] | |
// ignore trailing elements | |
let [first] = [1, 2, 3, 4]; | |
console.log(first); // outputs 1 | |
// or other elements | |
let [, second, , fourth] = [1, 2, 3, 4]; | |
// b. Object destructuring | |
let o = { | |
a: "foo", | |
b: 12, | |
c: "bar" | |
}; | |
let { a, b } = o; | |
// or: JavaScript normally parses a { as the start of block. | |
({ a, b } = { a: "baz", b: 101 }); | |
// one and remaining | |
let { a, ...passthrough } = o; | |
let total = passthrough.b + passthrough.c.length; | |
// c. Property renaming | |
// You can read a: newName1 as “a as newName1” | |
let { a: newName1, b: newName2 } = o; | |
// if I had written in different way | |
let newName1 = o.a; | |
let newName2 = o.b; | |
// d. Default values | |
function keepWholeObject(wholeObject: { a: string, b?: number }) { | |
let { a, b = 1001 } = wholeObject; | |
} | |
// e. Function declarations | |
type C = { a: string, b?: number } | |
function f({ a, b }: C): void { | |
// ... | |
} | |
// or for default value | |
function f({ a, b = 0 } = { a: "" }): void { | |
// ... | |
} | |
// f. Spread: the opposite of destructuring | |
let first = [1, 2]; | |
let second = [3, 4]; | |
let bothPlus = [0, ...first, ...second, 5]; | |
// Object spread which is useful for default values | |
let defaults = { food: "spicy", price: "$$", ambiance: "noisy" }; | |
let search = { ...defaults, food: "rich" }; | |
// not like this because food will be overwritten by defaults.food | |
let defaults = { food: "spicy", price: "$$", ambiance: "noisy" }; | |
let search = { food: "rich", ...defaults }; | |
// Object spread has two limits | |
// a. only includes an objects’ own, enumerable properties. | |
// lose methods when you spread instances of an object | |
class C { | |
p = 12; | |
m() { | |
} | |
} | |
let c = new C(); | |
let clone = { ...c }; | |
clone.p; // ok | |
clone.m(); // error! | |
// b. Typescript compiler doesn’t allow spreads of type | |
// parameters from generic functions. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment