Skip to content

Support to method decorator that change the method signature #49229

Open
@ZeeD

Description

@ZeeD

Suggestion

As the title said, I just started using typescript and I want to have a method decorator that is able to change the method signature and have tsc knew about the new signature.

🔍 Search Terms

I have seen a lot of works around class decorators, like #4881 but nothing specifically on supporting the behavior I have described.

✅ Viability Checklist

My suggestion meets these guidelines:

  • [*] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [*] This wouldn't change the runtime behavior of existing JavaScript code
  • [*] This could be implemented without emitting different JS based on the types of the expressions
  • [*] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

If not for other things, I would like to understand if there is some way to set the types of decoratorto signal the new decorated function signature.

📃 Motivating Example

As a minimal example, look at

function decorator(_target: any, _propertyKey: string, descriptor: PropertyDescriptor): void {
    const method = descriptor.value;
    descriptor.value = function wrapper(a: number, b: number, c: number): void {
        // here you have access to a, b, c
        console.log(a, b, c);
        return method.call(this, a, b, c);
    }
}

class C {
    @decorator
    method(a: number, b: number): void {
        // here you have access to a, b
        console.log(a, b);
    }
}

new C().method(1, 2, 3); // here you pass a, b and c

tsc sasys that there is an error in the last line, because it doesn't "know" that C.method have a new signature, after applying the decorator.

💻 Use Cases

I'm implementing a simple method wrapper that add a parameter to do pre-checks on method invocation itself

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions