Skip to content

Cannot get name of generic type passed into class #117

@anietieasuquo

Description

@anietieasuquo

I have the following;

import { reflect } from 'typescript-rtti';

interface Entity {
    id: string
}

interface User extends Entity {
    name: string;
}

class A<T extends Entity> {
        private instanceName: string;

        public constructor() {
            this.instanceName = reflect<T>().as('class').reflectedClass.getOwnProperty('name');
        }

        public getInstanceName(): string { return this.instanceName; }
}

const a = new A<User>();
console.log(a.instanceName);

Basically, I expected instanceName to be User but got Object.
What's the proper way to get the name of the generic type passed into the class?

I can't do reflect(T) as that reports the error: "TS2693: T only refers to a type, but is being used as a value here."

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions