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?
interface A{
a: boolean;
b: boolean;
}
interface B{
a: function;
b:function
}
interface A{
a: boolean;
b: boolean;
}
interface B{
a: function;
b:function
}
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...
Jump to solution
2 Replies
Solution
michaeldrotar
michaeldrotar6mo ago
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
interface Stub {
foo: unknown
bar: unknown
}

class MyStub implements Stub {
foo = 'a'
bar() { return this.foo }
}
interface Stub {
foo: unknown
bar: unknown
}

class MyStub implements Stub {
foo = 'a'
bar() { return this.foo }
}
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.
class MyStub implements Stub {
foo = ''
bar = ''
}

class MySpecialStub extends MyStub {
foo = 'hi'
// bar = 'bye' // optional, if not defined it's empty string from MyStub
}
class MyStub implements Stub {
foo = ''
bar = ''
}

class MySpecialStub extends MyStub {
foo = 'hi'
// bar = 'bye' // optional, if not defined it's empty string from MyStub
}
--- You can also do something similar with generics
type A = {
a: unknown
b: unknown
}

type B<T extends A> = {}

type C = B<{ a: string }> // error: Property 'b' is missing
type A = {
a: unknown
b: unknown
}

type B<T extends A> = {}

type C = B<{ a: string }> // error: Property 'b' is missing
but then you lose implements
interface D extends B<{ a: string, b: string }> {
c: unknown
}

class MyThing implements D {} // errors that 'c' is missing, but does not error about 'a' or 'b'
interface D extends B<{ a: string, b: string }> {
c: unknown
}

class MyThing implements D {} // errors that 'c' is missing, but does not error about 'a' or 'b'
--- 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.
choco
choco6mo ago
thank you for such detailed answer
Want results from more Discord servers?
Add your server