TypeScript: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
'''.gitignore''' | |||
<syntaxhighlight lang="ini"> | |||
dist | |||
node_modules | |||
npm-debug.log | |||
</syntaxhighlight> | |||
'''person.ts''' | |||
<syntaxhighlight lang="ts"> | |||
namespace People { | |||
export class Person { | |||
constructor(private firstName: string, private lastName: string){ | |||
//TODO | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
'''nation.ts''' | |||
<syntaxhighlight lang="ts"> | |||
namespace People { | |||
export class Nation { | |||
private population: Person[] = []; | |||
add(person: Person): void { | |||
this.population.push(person); | |||
} | |||
get count(): number { | |||
return this.population.length; | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
'''app.ts''' | |||
<syntaxhighlight lang="ts"> | |||
/// <reference path="person.ts" /> | |||
/// <reference path="nation.ts" /> | |||
import Person = People.Person; | |||
import Nation = People.Nation; | |||
const nation: Nation = new Nation(); | |||
nation.add(new Person('John', 'Lennon')); | |||
console.log(nation.count); | |||
</syntaxhighlight> | |||
'''package.json''' | |||
<syntaxhighlight lang="json"> | |||
{ | |||
"scripts":{ | |||
"compile":"./node_modules/.bin/tsc", | |||
"start":"npm run compile && node dist/app.js" | |||
}, | |||
"devDependencies":{ | |||
"typescript":"^2.0.9" | |||
} | |||
} | |||
</syntaxhighlight> | |||
'''tsconfig.json''' | |||
<syntaxhighlight lang="json"> | |||
{ | |||
"compilerOptions":{ | |||
"target":"es5", | |||
"outFile":"dist/app.js" | |||
}, | |||
"files":[ | |||
"app.ts" | |||
] | |||
} | |||
</syntaxhighlight> | |||
'''Build''' | |||
<syntaxhighlight lang="bash"> | |||
npm install | |||
npm start | |||
</syntaxhighlight> | |||
== References == | == References == | ||
* [http://kangax.github.io/compat-table/es5/ Compatibility Tables for ES5] | * [http://kangax.github.io/compat-table/es5/ Compatibility Tables for ES5] | ||
* [http://kangax.github.io/compat-table/es6/ Compatibility Tables for ES6] | * [http://kangax.github.io/compat-table/es6/ Compatibility Tables for ES6] | ||
* [https://gist.github.com/ducin/20c30803fcd28fd3fec38fcc6c05bd8a Beginning of TypeScript] | * [https://gist.github.com/ducin/20c30803fcd28fd3fec38fcc6c05bd8a Beginning of TypeScript] |
Revision as of 22:59, 8 May 2018
.gitignore
dist
node_modules
npm-debug.log
person.ts
namespace People {
export class Person {
constructor(private firstName: string, private lastName: string){
//TODO
}
}
}
nation.ts
namespace People {
export class Nation {
private population: Person[] = [];
add(person: Person): void {
this.population.push(person);
}
get count(): number {
return this.population.length;
}
}
}
app.ts
/// <reference path="person.ts" />
/// <reference path="nation.ts" />
import Person = People.Person;
import Nation = People.Nation;
const nation: Nation = new Nation();
nation.add(new Person('John', 'Lennon'));
console.log(nation.count);
package.json
{
"scripts":{
"compile":"./node_modules/.bin/tsc",
"start":"npm run compile && node dist/app.js"
},
"devDependencies":{
"typescript":"^2.0.9"
}
}
tsconfig.json
{
"compilerOptions":{
"target":"es5",
"outFile":"dist/app.js"
},
"files":[
"app.ts"
]
}
Build
npm install
npm start