Skip to content

Code duplication when declaring an ambient class implementing an interface #2653

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

Closed
knuton opened this issue Apr 7, 2015 · 3 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@knuton
Copy link

knuton commented Apr 7, 2015

Ambient class declarations implementing an interface require the user to repeat the type members declared in the interface.

For example with the interface

interface IFoo {
    bar: string;
    qux(): number;
}

an ambient declaration of Foo as

declare class Foo implements IFoo {}

fails with Class 'Foo' incorrectly implements interface 'IFoo'. Property 'bar' is missing in type 'Foo', while the following works as expected, but duplicates all declarations:

declare class Foo implements IFoo {
    bar: string;
    qux(): number;
}

While it should be possible to specialise when declaring an ambient class, it would be very convenient if unchanged type members would not need to be redeclared.

@qc00
Copy link

qc00 commented Apr 7, 2015

Use declare var Foo: {new(): IFoo}; (per "Class Decomposition" section in the manual)

@knuton
Copy link
Author

knuton commented Apr 7, 2015

Thanks, but this is only a partial solution:

Standard classes can be inherited from using extends; decomposed classes cannot.

https://github.com/Microsoft/TypeScript/wiki/Writing%20Definition%20Files#class-decomposition

@mhegazy
Copy link
Contributor

mhegazy commented Apr 7, 2015

I believe this is a duplicate of #340

@RyanCavanaugh had a good write-up on this issue in #340 (comment)

@mhegazy mhegazy added the Duplicate An existing issue was already created label Apr 7, 2015
@mhegazy mhegazy closed this as completed Apr 7, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 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

3 participants