'Typescript: Why Partial Utility Type doesn't work on Indexed Access Types
When using Partial on indexed access type, it doesn't set the properties to optional:
e.g.
class Klass {
prop = {
a: true,
b: 42,
};
func() {
let val: Partial<typeof this["prop"]>;
val = {a: false}; // error: Type '{ a: false; }' is not assignable to type 'Partial<this["prop"]>'.
val = {a: false, b: 13}; // ok
}
}
(see in typescript playground)
my use case is something like:
class Base {
setState(s: Partial<typeof this["state"]>){}
}
class Subclass extends Base {
state = {
a: true,
b: 42,
};
func() {
this.setState({a: false});
}
}
why is this not working and how can i get it to work?
Solution 1:[1]
You can use the name of the class and access prop without using this:
let val: Partial<Klass["prop"]>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tobias S. |
