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

Allow decorators to change input value #5647

Closed
CodySchaaf opened this issue Nov 13, 2015 · 1 comment
Closed

Allow decorators to change input value #5647

CodySchaaf opened this issue Nov 13, 2015 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@CodySchaaf
Copy link

Right now while using decorators the javascript seems to work, but the types are too strict.

As you can see here the following does not compile:

declare var _: any;
export function Throttle(milli: number) {
    interface Throttled extends Function {
            now: Function
    }
    return function<T extends Throttled>(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<Function>): TypedPropertyDescriptor<T> {
            let originalMethod = descriptor.value;
            // NOTE: Do not use arrow syntax here. Use a function expression in
            // order to use the correct value of `this` in this method
            descriptor.value = <any>_.throttle(function() {
                originalMethod.apply(this, arguments);
            }, milli);
            (<Throttled>descriptor.value).now = <any>function() {
                originalMethod.apply(this, arguments);
            };
            return <TypedPropertyDescriptor<T>>descriptor;
        }
}



    class Ctrl {

        constructor(scope: any) { 
            scope.$watch("foo", this.throttledByClassWatch);
            this.throttledByClassWatch.now() //unrecommended now call
        }
        @Throttle(100000)
        private throttledByClassWatch() {
        }
    }

I might have the types a little wrong but I have tried many other permutations. It seems to be because the expected type is declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void; and so the input T must match the output T.

Ideally the now could also inherit the generic type T so it could be type checked.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 13, 2015

looks like a duplicate of #4881. you might find my comment in #5627 (comment) helpful as a work around ins some cases, though would not work in your sample.

@mhegazy mhegazy closed this as completed Nov 13, 2015
@mhegazy mhegazy added the Duplicate An existing issue was already created label Nov 13, 2015
@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants