Skip to content

Instantly share code, notes, and snippets.

@xavdid
Created March 3, 2017 16:42
Show Gist options
  • Save xavdid/3ddeaa46af1fc583c2055f480f79a407 to your computer and use it in GitHub Desktop.
Save xavdid/3ddeaa46af1fc583c2055f480f79a407 to your computer and use it in GitHub Desktop.
Typescript Demo
import * as _ from 'lodash'; // or import _ = require('lodash')
interface HTTPRequest{
code: 200|400;
message: string;
}
class Employee {
name: string;
age: number;
bitmap: boolean[][]
constructor (name = 'David') {
this.name = name
this.bitmap = [[false, true], [true, false], [false, true, true]]
}
greet () {
return `Hi, ${this.name}!`
}
async setAge () {
this.age = await this.fetchAge();
}
flatBitmap () {
return _.flatten(this.bitmap)
}
fetchAge () {
// const response: HTTPRequest = request()
// if (response.code === 200) {}
// else {
// response.code
// }
return Promise.resolve(25)
}
}
const d = new Employee()
const w = new Employee('Wad')
console.log(d.greet())
console.log(w.greet())
d.setAge().then(data => {
console.log('age set!')
console.log(d.age)
})

I don't have a formal package.json here, but run this and you're all set:

npm i -g typescript npm i lodash @types/lodash

Enjoy!

{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"sourceMap": false,
"alwaysStrict": true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment