An interface has been created with a parameter that takes a generic type input named Data
export interface MyStructure<Data> {
id: string;
data: Data;
}
The goal is to allow the Data
type to be optional in order to support scenarios like:
function printId(obj: MyStructure): void {
console.log(obj.id);
}
In certain contexts like printId
, specifying the generic type for MyStructure<Data>
is not necessary.
The question arises on how to handle this situation: should the default be set to any
or unknown
, and what are the implications.
export interface MyStructure<Data = unknown> {
id: string;
data: Data;
}
export interface MyStructure<Data = any> {
id: string;
data: Data;
}