Created
December 13, 2016 01:22
-
-
Save Allan-Gong/8dad7543c2555b470a7bc8585d94ed3d to your computer and use it in GitHub Desktop.
Notes for typescript
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
// 1. Typed Tuple | |
let error: [number, string] = [123, 'Some Message']; | |
// Both correctly typed | |
let [code, message] = error; // code is number and message is string | |
let anotherCode = error[0]; // anotherCode is number | |
let anotherMessage = error[1]; // anotherMessage is string | |
// 2. String Literal Type | |
class Socket { | |
on (event: 'open' | 'message' | 'error' | 'close', cb) { // compiler errors when passed in string is not listed | |
// ... | |
} | |
} | |
let mySock = new Socket(); | |
socket.on('message', () => {}); | |
// 3. Void | |
function log(message: string): void { | |
// ... | |
} | |
// If function does not return any value, we should mark its return type to void. Otherwise, it defaults to any. | |
// 4. Implements Class | |
class Base1 { | |
method1 () {} | |
} | |
class Base2 { | |
method2 () {} | |
} | |
class MyClass implements Base1, Base2 { | |
constructor (private base1: Base1, private base2: Base2) {} | |
method1 () { return this.base1.method1(); } | |
method2 () { return this.base2.method2(); } | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment