Skip to content

Instantly share code, notes, and snippets.

@ducin
Last active September 21, 2022 20:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ducin/20c30803fcd28fd3fec38fcc6c05bd8a to your computer and use it in GitHub Desktop.
Save ducin/20c30803fcd28fd3fec38fcc6c05bd8a to your computer and use it in GitHub Desktop.
typescript namespaces example
dist
node_modules
npm-debug.log
/// <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);
namespace People {
export class Nation {
private population: Person[] = [];
add(person: Person): void {
this.population.push(person);
}
get count(): number {
return this.population.length;
}
}
}
{
"scripts": {
"compile": "./node_modules/.bin/tsc",
"start": "npm run compile && node dist/app.js"
},
"devDependencies": {
"typescript": "^2.0.9"
}
}
namespace People {
export class Person {
constructor(
private firstName: string,
private lastName: string
){}
}
}
{
"compilerOptions": {
"target": "es5",
"outFile": "dist/app.js"
},
"files": [
"app.ts"
]
}
@ducin
Copy link
Author

ducin commented Nov 12, 2016

Run:

npm install
npm start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment