Typescript: Function instead of interface name when using Reflect

Created on 5 Sep 2016  ·  4Comments  ·  Source: microsoft/TypeScript

Hi
I've a problem with using reflect in field decorators. I've created a decorator with below signature:

export function Inject() {
    return function (clazz, name) {
        .......
        let typeName: string = Reflect.getOwnMetadata("design:type", clazz, name).name;
        .........
    }
}

class MyClass{
    @Inject()
    private $q:ng.IQService
}

And I use @Inject on fields. It works when field type is class but when field type is interface, it returns Function not the name of interface. How can I fix it?

Question

All 4 comments

TypeScript types, e.g. interfaces, type aliases, etc.., do not exist at runtime. they are design-time constructs that the compiler uses to check the consistency of the code, and are removed when generating the final JS.

The refectMetaData feature only preserves metadata about runtime values. classes do have a runtime value, i.e. the constructor function, whereas interfaces do not.

if the compiler knows there is a runtime value packaging the type (classes or enums), then it will use it; if not, it will try find the best "guess". so number will be Number, string will be String, [] becomes Array, async functions become Promise, and if your interface has a call or construct signatue on it, it will be Function, if none of these matches, it becomes Object.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

siddjain picture siddjain  ·  3Comments

weswigham picture weswigham  ·  3Comments

fwanicka picture fwanicka  ·  3Comments

dlaberge picture dlaberge  ·  3Comments

MartynasZilinskas picture MartynasZilinskas  ·  3Comments