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 class decorators to extend type signature #25119

Closed
4 tasks done
sophistifunk opened this issue Jun 21, 2018 · 2 comments
Closed
4 tasks done

Allow class decorators to extend type signature #25119

sophistifunk opened this issue Jun 21, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@sophistifunk
Copy link

Search Terms

  • Decorators

Suggestion

Would it be possible for the type checker to know about changes made to a class via decorators?

Currently if you use a decorator to add a method to the prototype of a class constructor, you need to add a placeholder method that is overridden, or add a matching declaration to the interface with the same name. It's not terribly onerous, but it'd be good if we could leave it out.

Use Cases

This could be used to create typesafe mixins of specific interfaces, and possibly a generic mixin mechanism that would be easy to use.

Examples

function bar() {
    return "This is bar";
}

type Constructor<T> = { new(...args: any[]): T };

interface Decorated {
    bar():string;
}

// I want the type checker to pay attention to the return type of the decorator
function Decorate<T>(clazz:Constructor<T>): Constructor<T & Decorated> {
    clazz.prototype.bar = bar;
    return clazz as Constructor<T & Decorated>;
}

interface Foo extends Decorated{} // I want to leave this out

@Decorate
class Foo {
    foo() {
        return "This is Foo"
    }
}

const aFoo = new Foo();
console.log('foo told me', aFoo.foo());
console.log('foo also told me', aFoo.bar()); // This currently won't compile without the extra interface decl

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. new expression-level syntax)
@mhegazy
Copy link
Contributor

mhegazy commented Jun 21, 2018

Duplicate of #4881

@mhegazy mhegazy marked this as a duplicate of #4881 Jun 21, 2018
@mhegazy mhegazy added the Duplicate An existing issue was already created label Jun 21, 2018
@sophistifunk
Copy link
Author

Closing since it's a dupe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants