TS, nestjs

송은우·2022년 2월 15일
0

partial<type> 은 type에 하위에 해당하는 모든 것을 다 가능하게 함.

is 와 as는 둘이 비슷하게 생겼으므로 비슷한 역할일 것이라고 생각하는데
둘은 전혀 다른 역할을 합니다.

as
타입 단언 : as
굳이 개발자가 타입 지정을 하지 않아도 TS 컴파일러가 추론이 가능한 타입 추론 기능이 타입스크립트에는 있다.
이럴 때 as를 쓴다. 그리고 바로 이것이 타입 단언

// 타입 단언에는 두 가지 종류가 있다.
1: <Fish>pet
2: (pet as Fish)
이 때 1 번은 런타임과 컴파일 단계에서 모두 돌아가고
2 번은 컴파일 때만 돌아간다.

또 만약 리액트로 개발할 시 꺽쇠(<>)로 타입캐스팅 하는 것은 TSX 태그 문법이랑 비슷하기 때문에 as 를 추천한다.

is
타입 가드 : is
typeof 같은 걸로 타입 따져서 분기 처리 하는 역할을 TS 에선 is 이다.

if (isFish(Fish 타입 인 애)) { // isFish에서 Fish 타입이면 타입 가드에 의해서 조건문 통과
console.log(Fish 타입인 애); // OK
console.log(Bird 타입인 애); // Error
} else { // Bird 타입인 애가 들어가면 여기로!
console.log(Fish 타입); // Error
console.log(Bird 타입); // OK
}
from :https://velog.io/@dltjdwls100/TIL-Typescript-is%EC%99%80-as-%EC%9D%98-%EC%B0%A8%EC%9D%B4%EC%A0%90

any, unknown, never
https://xo.dev/typescript-unknown-any-never/

interface vs type
https://yceffort.kr/2021/03/typescript-interface-vs-type

decorator
https://dparkjm.com/typescript-decorators

getter, setter(accessor)
https://infoscis.github.io/2017/05/20/TypeScript-handbook-classes/

A.property 는 function만이 가지는 것. A가 function이어야 접근 가능함. 단 class도 function이다

method decorator
method decorator는 3가지 argument를 받는다.
첫 번째 argument: class의 prototype
두 번째 argument: class에서 해당 method의 key
세 번째 argument: property descriptor

class A {
  b: string = "Hello"

  get c(): string {
    return `${this.b} World!`
  }

  @LogError
  d(e: string): void {
    console.log(e)
  }
}

function LogError(target: any, key: string, 
	desc: PropertyDescriptor): void {
  console.log(target)
  console.log(key)
  console.log(desc)
}
{ c: [Getter], d: [Function (anonymous)] }
d
{
  value: [Function (anonymous)],
  writable: true,
  enumerable: true,
  configurable: true
}
function LogError(target: any, key: string, 
	desc: PropertyDescriptor): void {
  const method = desc.value // 기존의 method

  // 기존의 method가 error를 던졋을 때 
  //error handling 할 수 있도록 재정의
  desc.value = function () {
    try {
      method()
    } catch (err) {
      console.log("여기에 error handling logic 추가")
    }
  }
}

이렇게 수정한다면 조금 더 직관적이겠다 정도

function LogError(errorMessage: string) {
  return function (target: any, key: string, 
  	desc: PropertyDescriptor): void {
    const method = desc.value

    desc.value = function (e: string) {
      try {
        method(e)
      } catch (err) {
        console.log(errorMessage)
      }
    }
  }
}

hoc로 바꾼다면 이런 형태로 custom error

parameter decorator
parameter decorator는 3가지 argument를 받는다.
첫 번째 argument: class의 prototype
두 번째 argument: class에서 해당 method의 key
세 번째 argument: argument는 해당 parameter의 index 번호

nestjs

모듈은 어플리케이션의 일부임. 한 가지의 역할을 하는 앱정도로 생각하면 됨.
controller : url을 가져오고, 함수 실행 express router같음.
controller가 service를 호출하는 이유는 그냥 nestjs 가 원하는 구조일 뿐. controller는 그냥 url, service가 business logic
@Controller('movies') 라는 부분이 entry point가 됨.
nest에서 뭔가를 원한다면 요청해야 함.

single-responsibility principle 1개 모듈, 클래스, 함수는 1개만 해야 함.

import 를 쓰는 거는 nestjs의 방식이 아님.

provider :

profile
학생의 마음가짐으로 최선을 다하자

0개의 댓글