Is it possible to set a default value for the identifier
property in TypeScript based on whether the type extends from Entity
or not? Here's an example of what I'm trying to achieve:
export interface Entity {
id: number;
// ...
}
@Component({})
export class EpicComponent<T> {
@Input() data: T[] | null = null;
@Input() identifier: keyof T | null = T is of type Entity ? 'id' : null; // <--
}
In the above code snippet, when the generic type T
is an Entity
, the identifier
is automatically set to be the id
. But for other types, a different property might be needed as the identifier
. Is there a way to accomplish this in TypeScript, or is there a different approach that could work better? Any help or suggestions would be greatly appreciated.