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

Declarations for function-scoped class expressions #3129

Closed
awerlang opened this issue May 12, 2015 · 2 comments
Closed

Declarations for function-scoped class expressions #3129

awerlang opened this issue May 12, 2015 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@awerlang
Copy link

TLDR: How to generated .d.ts from function-scoped class expressions?

Consider the following snippet:

class MyService {
    constructor(private dependency) { }
    myMethod() {
        return this.dependency.someMethod();
    }
}

function myFactory(dependency) {
    return new MyService(dependency);
}

As you can see, I have to make a field for a dependency. This is so C# (I like C#) but I'd prefer the JavaScript way in JavaScript.

Then I make a closure:

function myFactory2(dependency) {
    return new class MyService {
        myMethod() {
            return dependency.someMethod();
        }
    };
}

I'm aware that I get a new constructor function each time and there's no prototype sharing, but I don't care. I see a high potential in this pattern for true information hiding. It is even better for inheritance. A base class could evolve without breaking derived classes.

To make it fit on TypeScript, however, automatic generation of declarations or somehow compiler being able to interpret this pattern is needed I think. An identifier for this factory type is needed.

One should be able to do the following then, based on the second snippet:

function MyController(myService: myFactory2) {
}
@ahejlsberg
Copy link
Member

This is slated for the 1.6 release: https://github.com/Microsoft/TypeScript/wiki/Roadmap.

@ahejlsberg
Copy link
Member

Also see #497.

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label May 12, 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