Is it possible to construct a TypeScript type that extracts the property names that are unique to a specific class, that it did not inherit from its parent class? For instance, this works if I know the parent class (Playground Link):
class Parent {
hello = 'hello'
}
class Child extends Parent {
world = 'world'
}
type OwnProps<Base, T extends Base> = Exclude<keyof T, keyof Base>
declare const ownProps: OwnProps<Parent, Child> // "world"
However, I would like a way to accomplish the same type of thing, but without explicitly specifying the base class, something like this:
type OwnProps<T, Base = T extends ???> = Exclude<keyof T, keyof Base>
declare const ownProps: OwnProps<Child> // "world"
Is this possible? Is there some kind of conditional magic I can use in place of ???
that would let me infer
types that the Child
class extends?
Inheritance allows us to define a class that inherits all the methods and properties from another class. Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class.
Since the method is not virtual, there is no problem to have the same name. Which one is called depends merely on the type of the reference.
Yes, parent and child classes can have a method with the same name.
Example: Declaring a classThe var keyword is not used while declaring a field. The example above declares a constructor for the class. A constructor is a special function of the class that is responsible for initializing the variables of the class. TypeScript defines a constructor using the constructor keyword.
Unfortunately it looks impossible. TypeScript uses structural type system and a type doesn't store any information about class hierarchy, i.e. the Child
type it's just { hello: string, world: string }
.
You can also look at the other similar question: TypeScript: Get Type of Super Class?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With