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

compiler throws a false positive on interface conformance #7103

Closed
bcherny opened this issue Feb 16, 2016 · 4 comments
Closed

compiler throws a false positive on interface conformance #7103

bcherny opened this issue Feb 16, 2016 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@bcherny
Copy link

bcherny commented Feb 16, 2016

is there a better way to express this, without explicitly listing out every member of the interface?

interface Foo {
    a: string
}

// ok
class Bar implements Foo {
    constructor (public a: string) {}
}

// Error: Class 'Baz' incorrectly implements interface 'Foo'.
//        Property 'a' is missing in type 'Baz'
class Baz implements Foo {
    constructor (a: Foo) {
        Object.assign(this, a)
    }
}

demo: http://www.typescriptlang.org/Playground#src=interface%20Foo%20%7B%0A%09a%3A%20string%0A%7D%0A%0A%2F%2F%20ok%0Aclass%20Bar%20implements%20Foo%20%7B%0A%09constructor%20(public%20a%3A%20string)%20%7B%7D%0A%7D%0A%0A%2F%2F%20Error%3A%20Class%20'Baz'%20incorrectly%20implements%20interface%20'Foo'.%0A%2F%2F%20%20%20%20%20%20%20%20Property%20'a'%20is%20missing%20in%20type%20'Baz'%0Aclass%20Baz%20implements%20Foo%20%7B%0A%09constructor%20(a%3A%20Foo)%20%7B%0A%09%09Object.assign(this%2C%20a)%0A%09%7D%0A%7D

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Feb 17, 2016
@RyanCavanaugh
Copy link
Member

There isn't a better way to do this. See also #340 and many duplicates that reference it.

@jeffreymorlan
Copy link
Contributor

@RyanCavanaugh: In TypeScript 1.7+ it seems like you can do this with a merged class/interface declaration:

interface Baz extends Foo {}
class Baz {
    constructor (a: Foo) {
        Object.assign(this, a);
        console.log(this.a); // no error here
    }
}

@RyanCavanaugh
Copy link
Member

@jeffreymorlan you're very right! I forgot about that

@bcherny
Copy link
Author

bcherny commented Feb 17, 2016

thanks @jeffreymorlan! still not pretty, but it's an improvement.

@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
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants