[NODEJS] TypeScript with Node.js

[NODEJS] TypeScript with Node.js ?

  1. 프로젝트 폴더 생성

  2. init

    1
    npm init -y
  3. 타입스크립트 설치

    1
    npm install -g typescript
  4. 타입설정 초기화

    1
    tsc — init
  5. 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/**/*"
    ]
    }

  1. 폴더생성 ./src ./dist

  2. .src/test.ts 작성

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    class 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());
  3. tsc 컴파일

    1
    tsc
  4. js 파일 실행

    1
    node ./dist/test.js
  5. 결과물 확인
    결과