[NODEJS] TypeScript with Node.js ?
프로젝트 폴더 생성
init
1
npm init -y
타입스크립트 설치
1
npm install -g typescript
타입설정 초기화
1
tsc — init
tsconfig.json 수정
1
2
3
4
5
6
7
8
9
10
11
12
13
14{
"compilerOptions": {
/* Basic Options */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"outDir": "./dist", /* Redirect output structure to the directory. */
"strict": true, /* Enable all strict type-checking options. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
},
"include": [
"./src/**/*"
]
}
폴더생성 ./src ./dist
.src/test.ts 작성
1
2
3
4
5
6
7
8
9
10
11class Person {
private hello: String = "";
constructor(private name: String) {
this.hello = name + " Hello~";
}
public getHello(): String{
return this.hello;
}
}
let person: Person = new Person("namgi");
console.log(person.getHello());tsc 컴파일
1
tsc
js 파일 실행
1
node ./dist/test.js
결과물 확인