강의

멘토링

커뮤니티

Inflearn Community Q&A

No author

This post's author information has been deleted.

[Renewal] TypeScript All-in-One: Part 1. Basic Grammar

Optional, generic default

제네릭 extends 함수로 한 경우

Written on

·

324

·

Edited

0

function test<T extends (a: number, b: string) => boolean>(callback: T): void {
  return;
}

const callback = (a: number) => {return true};
const callback2 = (a: string) => {return true};
const callback3 = () => {return true};

test(callback); // OK
test(callback2); // ERROR
test(callback3); // OK

playground에서 위 코드 입력시 callback2에서만 오류가 나고 나머진 통과됩니다.

제네릭에선 a, b 옵셔널이 아닌 두 매개변수를 정했는데 왜 빈값 혹은 1개만 넣어도 충족되는지 궁금합니다.

typescript

Answer 1

1

zerocho님의 프로필 이미지
zerocho
Instructor

콜백 함수인 경우는 기본적으로 매개변수 생략 가능합니다. 자바스크립트 생각해보시면

addEventListener('click', () => {})

addEventListener('click', (e) => {})

둘 다 되는 것처럼요.

No author

This post's author information has been deleted.

Ask a question