컴퓨터/Javascript

require으로 모듈 import 할때, 타입스크립트 IntelliSense / 자동완성 작동이 안된다.. & Json 세팅

no title

타입스크립트 & JSON 설정

https://skyksit.com/programming/nodejs/nodejs-set-typescript/
http://daplus.net/javascript-npm-스크립트를-순차적으로-실행/

1. 타입스크립트 json

{
  "compilerOptions": {
    "target": "ES5",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    // "lib": [],                                        /* Specify a set of bundled library declaration files that describe the target runtime environment. */

    /* Modules */
    
    "module": "commonjs",                                /* Specify what module code is generated. */
    "rootDir": "./src",                                  /* Specify the root folder within your source files. */
    "moduleResolution": "node",                       /* Specify how TypeScript looks up a file from a given module specifier. */

    /* Emit */
    "sourceMap": true,                                /* Create source map files for emitted JavaScript files. */
    "outDir": "./public",                                   /* Specify an output folder for all emitted files. */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables
    /* Type Checking */
    "strict": true,                                      /* Enable all strict type-checking options. */
  },
  "include" : ["src/**/*"],
  //모든 스크립트는 src 로 넣은 구조여야 한다
  "exclude" : ["node_modules"],
}

2. json

{
  "name": "typechain",
  "type": "commonjs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "tsc && node ./public/index.js"
    //&& 연산자로 node 명령을 여러줄 한꺼번에 실행시키게 할 수 있다.
  },
  "author": "",
  "license": "ISC",
}

타입스크립트 commonJs 에서 자동완성이 안되는 오류.

https://joshua1988.github.io/ts/usage/modules.html#export

모듈 import의 문제다
아래와 같이 적어야 자동완성이 된다.

import * as fs from "fs";
//혹은
import fs = require("fs");

만약 commonJs

  • var/let/const ver = require("모듈") 형식으로 적으면 사용 못하는것은 아닌데.
  • 자동완성이 안된다.
let fs = require("fs");

그러니.. Typescript에서는 import로 시작해서 모듈 적용을하자.. ㅠㅠ