-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Cannot use a class with private fields as an interface for object literal #12859
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
Comments
Because |
Ok, then it should be legal to do: const mock: Foo = {
secret: 42,
bar() { return 2; },
baz() { return 3; },
} but that causes:
There should be some way of implementing that interface with an object literal. |
Again, you'll need a type assertion here. The issue in this case is that a |
I would argue the whole point of structural typing is that you can do just that- if the shape fits, it should work. More to the point, I feel like you're guarding against a case that hardly arises in real practice, but precludes basic testing practices that have been going on in the JS community since forever. As for the type assertion, it's rather unfortunate that I have to disable all type checking, especially when type checking is what I want in my test. I want to be certain that my mocks uphold the shape of objects my systems under test expect. Of course, I can extract the "header interface" and have the class and mock implement that (as one does in Java/C#), but that seems rather to defeat the point that classes are interfaces. |
TypeScript Version: 2.2.0-dev.20161212
Code
Expected behavior:
Code compiles with no issue.
Actual behavior:
Comments:
Intriguingly, there is no way to use
Foo
as an interface without coercing it throughany
(afaict). I think the "classes are interfaces" aspect of TypeScript is incredibly powerful, but this behavior ruins it.The text was updated successfully, but these errors were encountered: