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

Class without setter property incorrectly allowed to implement interface #20093

Closed
MastroLindus opened this issue Nov 17, 2017 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@MastroLindus
Copy link

TypeScript Version: reproduced on 2.7.0-dev, and on 2.2.1

Code

interface A {
  readonly foo: string;
}

interface B {
  foo: string;
}

class C implements A,B {
  public get foo() { return "bla";}
}

const baz = (p: B) => p.foo = "blabla";

baz(new C());

Expected behavior:
-Compiler complains that C doesn't implement properly B since it has no setter (and now we have a proper way to define an interface with a readonly property as A shows).
-Compiler complains if you try to pass a C to function baz.

In general with or without the "implements" declaration, C should not be assignable to B if it doesn't implement a setter for foo.

Actual behavior:
No Compile Errors.
Runtime error when baz tries to set the foo property.

I am not really sure this is a bug or some design choice, but I would definitely expect the type system to catch this, especially since we have the "readonly" keyword to mark properties that cannot be set.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 17, 2017

Please see #13347

@mhegazy mhegazy added the Duplicate An existing issue was already created label Nov 17, 2017
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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