강의

멘토링

커뮤니티

Inflearn Community Q&A

tubowangin4195's profile image
tubowangin4195

asked

[Code Factory] [Beginner] Complete Typescript Course from Code Factory in Just 8 Hours

Using Generics in Functions

instantiator 질문

Written on

·

144

0

function instantiator<T extends { new (...args: any[]): {} }>(constructor: T, ...args: any) {
  return new constructor(...args);
}

console.log(instantiator(Idol, '아이유', 23));
console.log(instantiator(Car, 'BMW', 'M3'));

여기에서 instantiator의 parameter에서의 constructor parameter 변수는,

 

사실상 생성자의 의미보다는 class 정의 자체(T의 형식을 가진) 를 넘겨준다고 봐도 되는건가요?

typescript

Answer 1

0

codefactory님의 프로필 이미지
codefactory
Instructor

안녕하세요!

constructor는 T 타입이 맞습니다.

그리고 T 타입은 new 키워드로 생성 가능한 객체를 상속받기 때문에 클래스가 될 수 있습니다.

class 정의 자체를 넘겨준다는건 어떤 뜻인지 제가 이해를 잘 못했습니다.

감사합니다!

tubowangin4195's profile image
tubowangin4195

asked

Ask a question