#Typescript "implements"?

1 messages · Page 1 of 1 (latest)

west bloom
#

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
}
plush viper
#

interfaces are the types, not the implementation, so they aren't really designed to be checked like that

west bloom
plush viper
#

don't really think so

#

if all the values are the same, you can make them align much more easily though

#
type Keys = "a" | "b";
type A = Record<Keys, boolean>;
type B = Record<Keys, () => void>;
west bloom
#

!resolve

flat cliff
#

@west bloom

left pagodaBOT
#
sandiford#0

Preview:```ts
interface A {
a: boolean
b: boolean
}
interface B {
a: Function
b: Function
}
interface C {
a: Function
}

// checks
type BContainsAllKeysFromA<
A extends object,
B extends {[K in keyof A]: Function}

= B

type OK1 = BContainsAllKeysFromA<A, B>
...```

flat cliff
#

You can do checks like this

#

or just generate B from A with a mapped type

#

But I think that a mapped type will produce a type not an interface, which has just slightly differnt behaviour.

west bloom
#

wow it is still what i need as well

#

thank you Bert