Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Provide type information to property decorators #11866

Closed
PyroVortex opened this issue Oct 26, 2016 · 1 comment
Closed

Suggestion: Provide type information to property decorators #11866

PyroVortex opened this issue Oct 26, 2016 · 1 comment
Labels
Domain: Decorators The issue relates to the decorator syntax Revisit An issue worth coming back to

Comments

@PyroVortex
Copy link

Issue
Currently there is no way to enforce type-safety on property decorators.

Current property decorator syntax

function defaultValue<T>(value: T) {
    // No way to enforce that target[propertyKey] is of type T
    return function (target: any, propertyKey: string) {
        Object.defineProperty(target, propertyKey, { value: value });
    };
}

class Foo {
    @defaultValue(4)
    x: number; // Compiles without error

    @defaultValue('foo')
    y: number; // Also compiles without error, no current way to enforce type safety
}

Proposed property decorator syntax

function defaultValue<T>(value: T) {
    return function (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>) {
        // Note that descriptor has the value (<TypedPropertyDescriptor<T>>undefined)
        Object.defineProperty(target, propertyKey, { value: value });
    };
}

class Foo {
    @defaultValue(4)
    x: number; // Compiles without error

    @defaultValue('foo')
    y: number; // Error: argument of type 'TypedPropertyDescriptor<number>' is not
               // assignable to parameter of type 'TypedPropertyDescriptor<string>'
}

The descriptor parameter could really be anything; its only purpose is to provide the compiler with type information about the property being decorated. I used TypedPropertyDescriptor<T> here for consistency with method and accessor decorators. Current downlevel (ES5) emit already passes undefined explicitly as the third parameter to the decorator function.

@PyroVortex PyroVortex changed the title Provide type information to property decorators Suggestion: Provide type information to property decorators Oct 26, 2016
@mhegazy mhegazy added the Domain: Decorators The issue relates to the decorator syntax label Apr 27, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Apr 27, 2017

Property decorators do not get PropertyDiscriptor's like method decorators do. that would be the way to pass the type to the function.

The reason this does not happen is that these properties are not defined until the constructor runs, so the value can not be computed ahead of time as it is an expression and can have side effects.

It is possible that we can do this when Public class fields TC39 proposal is ratified. this will give us some standard framework to represent initialization of a property that will run at construction time.

@mhegazy mhegazy added the Revisit An issue worth coming back to label Apr 27, 2017
@mhegazy mhegazy closed this as completed Apr 27, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Domain: Decorators The issue relates to the decorator syntax Revisit An issue worth coming back to
Projects
None yet
Development

No branches or pull requests

2 participants