Typescript "implements"?
Hello, guys, i have two interfaces that have same name fields but different implementation, but i want when i create new interface without all fields from A in new interface, it should throws an error any help please?
Solution:Jump to solution
What you're asking for is almost like an
interface B implements A {}
- which doesn't work.
I tried a few different things. I'm not sure there's a way to do exactly what you're asking.
The closest I can get is a single interface with unknown types, where the class can then define the types however it wants...2 Replies
Solution
What you're asking for is almost like an
interface B implements A {}
- which doesn't work.
I tried a few different things. I'm not sure there's a way to do exactly what you're asking.
The closest I can get is a single interface with unknown types, where the class can then define the types however it wants
And if baz: unknown
is added to the interface then MyStub
will have an error that it needs it added as well
You could then extend those classes another layer to get close to what you're asking for, but keys that are not re-defined will inherit from their parents. I'm assuming this is also not ideal.
---
You can also do something similar with generics
but then you lose implements
---
Maybe someone else will get closer, but if you explain more of your exact scenario there might also be a better pattern for it.
Using runtime errors rather than TS ones could certainly handle this if that's a better compromise. Other languages have virtual classes that get close to this too. I personally avoid classes. Using the types and then defining objects that conform to those types would get you there if you can give up on implements
and having classes.thank you for such detailed answer