Is it possible to get name of the interface as a string? Is it possible to get name of the interface as a string? typescript typescript

Is it possible to get name of the interface as a string?


This is not possible. TypeScript doesn't generate any code for interfaces and there's nothing like the nameof operator.


You can if you map all your interfaces to their name, like below.

interface Blueberry { strawberry: boolean; }interface Car { vehicle: number; }interface Interfaces {    Blueberry: Blueberry;    Car: Car;}

Then you can get the names of an interface like so:

export type RemoveNeverProps<T> = {    [K in Exclude<        keyof T, ({            [P in keyof T]: T[P] extends Function ? P : never        })[keyof T]>    ]: T[K]};export type IncludeProp<T extends object, E> =    RemoveNeverProps<{ [K in keyof T]: T[K] extends E ? T[K] : never }>;type Nameof<I extends Interfaces[keyof Interfaces]> =    keyof IncludeProp<Interfaces, I>;