I have a type State
in which all fields are required:
JavaScript
x
6
1
type State = {
2
title: string,
3
items: [],
4
isActive: boolean,
5
};
6
I need to create a new type with all properties the same as in State
but not required:
JavaScript
1
9
1
type StateUpdater = {
2
title?: string,
3
items?: [],
4
isActive?: boolean,
5
};
6
7
// ({title: 'hello'}: State) — INVALID
8
// ({title: 'hello'}: StateUpdater) — OK
9
How can I implement this similarly to the following Flow pseudocode?
JavaScript
1
2
1
type StateUpdater = State.mapKeyValue(() => typeKey?: typeValue)
2
Advertisement
Answer
You can use $Shape utility:
JavaScript
1
8
1
type State = {
2
title: string,
3
items: [],
4
isActive: boolean,
5
};
6
7
type StateUpdater = $Shape<State>;
8
$Shape
Copies the shape of the type supplied, but marks every field optional.