Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

이펙티브 타입스크립트 6장 #22

Open
humonnom opened this issue Nov 13, 2022 · 0 comments
Open

이펙티브 타입스크립트 6장 #22

humonnom opened this issue Nov 13, 2022 · 0 comments
Assignees

Comments

@humonnom
Copy link
Collaborator

humonnom commented Nov 13, 2022

아이템 48

  • 주석 관련 내용(TSDoc)

아이템 49

콜백에서 this에 대한 타입 제공하기

this 바인딩 복습

  • this는 자바스크립트 엔진에 의해 암묵적으로 생성된다.

  • 함수를 호출하면 동적으로 바인딩이 결정되고, this가 arguments와 함께 함수로 전달된다.

  • 메서드는 객체에 포함된 것이 아니라 독립적으로 존재하는 별도의 객체이다.

  • 다른 객체에 할당하여 호출하면 다른 결과를 보일 수 있다.

  • 일반 변수에 할당하여 일반 함수로 호출할 수 있다.

책에서의 외부 변수에 할당한 후 호출하는 예시

window.name='modified';

class A {
    name = 'a';
    
    log() {
        console.log(this.name);
    }    
};

const a = new A();

a.log(); // 'a'

var method = a.log;

method(); // undefined => type error

window.method(); // 'modified'

아이템 50

  • 오버로딩 타입 보다는 조건부 타입을 사용하기

  • number 혹은 string을 인자로 받고 각각 number, string을 리턴하는 경우 => 타입 정보를 추가하려면?

    • 타입 스크립트의 함수 오버로딩으로 구현
    • 제너릭을 사용하여 구현
    • 여러가지 타입 선언으로 구분
    • 조건부 타입 => 타입 공간의 if문
  • 이 방법들 중에 가장 적합한 것은 조건부 타입이라는 내용


아이템 51

  • 어떤 라이브러리의 타입이나 구현 둘 중에 하나에만 의존하는 경우, 의존성을 피하기 위해 필요한 코드만 복사해서 붙여 넣을 수 있다.
  • 이것을 mirroring이라고 하고, 의존성 분리를 위해 사용하는 기법이다.

아이템 52

  • 타입 선언에 대한 테스팅

  • 테스트를 작성할 때

    • 반환값도 테스트 해야한다.(실행에서의 오류를 잡기 위해)
    • 일반적으로 불필요한 타입 선언도 테스트 코드 관점에서는 중요한 것도 있다.
    • 헬퍼 함수 활용(동일성 체크가 아닌 할당 가능성 체크 주의)
    • 블랙박스 형식의 타입 검사가 있고, 내부 세부 사항까지 테스트 할 수도 있다.
  • 이런 문제적을 인지하고, 외부의 타입 검사 도구를 사용하는 것이 좋다.

@humonnom humonnom self-assigned this Nov 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant