Last active
September 21, 2022 20:26
-
-
Save ducin/20c30803fcd28fd3fec38fcc6c05bd8a to your computer and use it in GitHub Desktop.
typescript namespaces example
This file contains 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
dist | |
node_modules | |
npm-debug.log | |
This file contains 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
/// <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); |
This file contains 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
namespace People { | |
export class Nation { | |
private population: Person[] = []; | |
add(person: Person): void { | |
this.population.push(person); | |
} | |
get count(): number { | |
return this.population.length; | |
} | |
} | |
} |
This file contains 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
{ | |
"scripts": { | |
"compile": "./node_modules/.bin/tsc", | |
"start": "npm run compile && node dist/app.js" | |
}, | |
"devDependencies": { | |
"typescript": "^2.0.9" | |
} | |
} |
This file contains 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
namespace People { | |
export class Person { | |
constructor( | |
private firstName: string, | |
private lastName: string | |
){} | |
} | |
} |
This file contains 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
{ | |
"compilerOptions": { | |
"target": "es5", | |
"outFile": "dist/app.js" | |
}, | |
"files": [ | |
"app.ts" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run: