Search code examples
typescriptgenericsdecoratorreflect-metadata

Getting a modified type from a class marked by decorator


I have a class defined like:

class Person {
    @OneToOne()
    pet: Animal;
}

Is there a way to get a modified type which looks like this? (Adding {propertyKey}Id: string to properties by marking it with a decorator)

// Using generics
type ModifiedPerson = SomeGeneric<Person>

// Or maybe using functions?
type ModifiedPerson = typeof someFunc(Person);

// Equivalent as:
type ModifiedPerson = {
    pet: Animal,
    petId: string,
}

Solution

  • What you are asking is not properly possible with typescript today. There is a pending issue open since 2015 on GitHub.

    @Decorators have no effect on type.