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

Docs: Decorators for class decorator needs to either be explicit about the type or note that new interface isn't inferred #443

Closed
orta opened this issue Apr 3, 2020 · 3 comments

Comments

@orta
Copy link
Contributor

orta commented Apr 3, 2020

Page URL: https://www.typescriptlang.org/docs/handbook/decorators.html

Issue:

function classDecorator<T extends { new(...args: any[] ) : {} }>(constructor: T) {
    return class extends constructor {
        newProperty = "new property";
        hello = "override";
    }
}

@classDecorator
class Greeter {
    property = "property";
    hello: string;
    constructor(m: string) {
        this.hello = m;
    }
}

const a = new Greeter("world")
a.newProperty

a.newProperty errors because a isn't inferred to have newProperty from the classDecorator.

Recommended Fix: Either explicitly add the type in the code sample, or state in the docs

re microsoft/TypeScript#4881

@zackdotcomputer
Copy link

I wound up here because I encountered this issue IRL - the example on the website led me to misunderstand the capabilities of the class decorator. Agree this example should be corrected so that it doesn't imply that decorators can define new properties on the object since that is not exactly the case right now (at least not in a way that typescript's compiler is happy about).

@jmar777
Copy link

jmar777 commented Dec 9, 2020

Bumping this issue, as the docs continue to suggest that class decorators can reshape the target in a TypeScript-aware way. Given the number of related GitHub issues / StackOverflow questions, there seems to be a lot of avoidable wheel-spinning if the documentation were clearer on this point.

I would suggest a new "NOTE" along the lines of:

NOTE TypeScript supports the runtime semantics of the decorator proposal, but does not currently track changes to the shape of the target. Adding or removing methods and properties, for example, will not be tracked by the type system.

Likewise, it may be best to remove the following line from the code example, as TypeScript will be unaware of newProperty:

    newProperty = "new property";

orta added a commit that referenced this issue Mar 10, 2021
@typescript-bot
Copy link
Collaborator

Hello! As per #2804, we are automatically closing all open issues. Please see #2804 for a description of what issues and PRs can be accepted going forward.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants