래스로 사용되는 경우 각 타입 매개변수에 대한 타입 인수를 제공해야 한다고 이야기했습니다.
타입 매개변수 선언 뒤에
=
와 기본 타입을 배치해 타입 인수를 명시적으로 제공할 수 있습니
다. 기본값은 타입 인수가 명시적으로 선언되지 않고 유추할 수 없는 모든 후속 타입에 사용됩
니다.
다음
Quote
인터페이스는 값이 제공되지 않는 경우 기본값이
string
인
T
타입 매개변수를 받
습니다.
explicit
변수는 명시적으로
T
를
number
로 설정하는 반면
implicit
와
mismatch
의
T
는
string
이 됩니다.
interface Quote<T = string> {
value: T;
}
let explicit: Quote<number> = { value: 123 };
let implicit: Quote = { value: "Be yourself. The world worships the original." };
let mismatch: Quote = { value: 123 };
// ~~~~~ ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.