Skip to content

Allow class decorators to extend type signature #25119

Description

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)

Activity

  1. mhegazy commented on Jun 21, 2018

    @mhegazy
    Contributor

    Duplicate of #4881

  2. sophistifunk commented on Jun 25, 2018

    @sophistifunk
    Author

    Closing since it's a dupe.

  3. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions