Answer the question
In order to leave comments, you need to log in
The topic in the TypeScript help is Generic parameter defaults. What is T?
Hello. I’m looking at a project in TypeScript, I saw a construction that I didn’t understand. Googled the certificate. I looked, but still did not understand what is T in the end? An example of the code I'm looking at:
export type IAllParams =
| IPersonParams
| ICompanyParams
export interface ISomeType<T extends IBaseParams = IAllParams> {
identifier?:string;
}
export interface IBaseParams {
title?:string;
}
declare function create(): Container<HTMLDivElement, HTMLDivElement[]>;
declare function create<T extends HTMLElement>(element: T): Container<T, T[]>;
declare function create<T extends HTMLElement, U extends HTMLElement>(
element: T,
children: U[]
): Container<T, U[]>;
declare function create<T extends HTMLElement = HTMLDivElement, U = T[]>(
element?: T,
children?: U
): Container<T, U>;
Answer the question
In order to leave comments, you need to log in
one)
function a<T extends SomeType>(arg: T): void {
// ...
}
this construction means that the generic type T must have all the SomeType properties function a<T = SomeType>(arg: T): void {
// ...
}
in this case, we set the default value for the generic type T, that is, by default it will be equal to SomeType, similar to the default parameters in normal javascriptfunction a<T extends SomeType = DefaultType>(arg: T): void {
// ...
}
merging everything together, we get that the generic type T must be an extension of SomeType and defaults to DefaultType (which must also be an extension of SomeType)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question