Provided
class A {
props: {
bool?: boolean,
test: string
} = {
test: 'a'
};
setProps(newPropertiesr: Partial<this['props']>) {
}
a() {
this.setProps({
bool: false
});
}
}
An error is encountered
The argument
{ bool: false; }
cannot be assigned to the parameter of type.(2345)Partial<this["props"]>
You can see the issue here: here in TypeScript Playground
If the A.props
property is defined as {bool?: boolean, test?: string}
with test
being optional, it works fine. It seems like the Partial<>
is being disregarded.
Is there a way to make the Partial
function properly with this
?
The above scenario illustrates a basic case, for better understanding: I have numerous classes with properties that extend from a main class containing a setProps
method and I want to correctly type that inherited setProps
.
class Parent {
props: any;
setProps(newData: Partial<this['props']>) {/*...*/}
}
class A extends Parent{
props: {
bool?: boolean,
test: string
} = /* ... */;
a() {
this.setProps({
bool: false
});
}
}