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
What you're asking for is almost like an
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
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
---
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
interface B implements A {} - which doesn't work.
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 wellYou 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.