notes

[TypeScript] 개론 및 개발환경 구축 (OOP/EACCESS: permission denied)

서울의볼 2024. 4. 14. 02:51

요약

자바스크립트(자스)엔 여러 문제점이 있었음.

1. 실행 시간에 결정되는 변수 타입

2. 약한 타입 체크

3. 물렁한 객체

 

타입스크립트(타스)로 위 문제점들을 효과적으로 보완할 수 있음.

1. 컴파일 시간에 변수의 타입을 체크해줌

2. 코드 입력시 에러 메세지 반환

3. 없는 객체 역시 에러 메세지 반환

 

추가로, 타스는 객체지향프로그래밍(OOP)에 큰 메리트가 있음.

예를 들어,

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  growOlder() {
    this.age += 1;
  }
}

const spartan = new Person('Spartan', 30);
spartan.age = 25; // 외부에서 age 속성을 마음대로 조작할 수 있어요! 뜻밖에 회춘?
spartan.growOlder();
console.log(spartan.age); // 결국 1살을 더 먹었지만 르탄이는 26세

자스의 경우 프로퍼티(속성)에 대한 접근 제한이 없는 반면, 타스는 외부 접근을 제어할 수 있음:

class Person {
  private name: string;
  private age: number; // age가 private인 것을 주목하세요!

  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }

  public growOlder(): void {
    this.age += 1;
  }
}

const spartan = new Person('Spartan', 30);
spartan.age = 25; // Error: Property 'age' is private and only accessible within class 'Person'.
spartan.growOlder();
console.log(spartan.age); // Error: Property 'age' is private and only accessible within class 'Person'.

또한, 타스의 d.ts 확장자 선언 파일은 외부 모듈 타입 정보를 제공할 수 있어 자스 라이브러리도 안전하게 사용 가능함.

 

 

---

개발환경 구축 과정 중에 아래의 문제점이 있었음:

Mac:typescriptLecture $ npm i typescript -g
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules/typescript
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/typescript'
npm ERR!  [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/typescript'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/usr/local/lib/node_modules/typescript'
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in: /Users/mac/.npm/_logs/2024-04-13T17_23_56_829Z-debug-0.log

찾아본 해결 방법으론, 특정 패키지 설치시 보안상 권한 이슈로 터미널에서 아래의 커맨드를 치면 된다고 했음

sudo chown -R $USER /usr/local/lib/node_modules

 

그러나 또 문제가 발생함:

Mac:typescriptLecture $ npm i typescript -g
npm ERR! code EACCES
npm ERR! syscall symlink
npm ERR! path ../lib/node_modules/typescript/bin/tsc
npm ERR! dest /usr/local/bin/tsc
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, symlink '../lib/node_modules/typescript/bin/tsc' -> '/usr/local/bin/tsc'
npm ERR!  [Error: EACCES: permission denied, symlink '../lib/node_modules/typescript/bin/tsc' -> '/usr/local/bin/tsc'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'symlink',
npm ERR!   path: '../lib/node_modules/typescript/bin/tsc',
npm ERR!   dest: '/usr/local/bin/tsc'
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in: /Users/mac/.npm/_logs/2024-04-13T17_29_30_420Z-debug-0.log

 

보아하니 그냥 배쉬에 sudo npm i typescript -g 치니까 바로 깔렸음.