Typescript 절대 경로 설정

Dongjun Ahn·2022년 4월 21일
0

프로젝트 디렉토리 구조는 어느정도 구조가 잡혔고,
본격적인 개발 시작전에 절대경로 설정을 해보자.

절대경로(alias) 설정을 해두면
import/export 시에 경로를 깔끔하게 만들수 있다.

import { bar } from '../../../../bar';

import { bar } from '@src/bar'; 
또는
import { bar } from 'bar';

Typescript 프로젝트는 tsconfig.json 에 추가하면 된다.
JS 프로젝트는 jsconfig.json

{
  "compilerOptions": {
    ...
    "baseUrl": ".", 
    "paths": {
      "@/*": [  
        "src/*"
      ]
    }
  }
}

위와 같이 설정하면

import * as api from '@/api';
import * as components from '@/components';

식으로 절대경로를 설정할수 있다.

profile
Front-end Developer

0개의 댓글