-
-
Notifications
You must be signed in to change notification settings - Fork 679
Consider UnwrapPartial<T> as a way to revert the Partial<T> modifier #1294
Copy link
Copy link
Closed
Labels
Description
Type description + examples
Let’s consider the following case:
type MyKeyType = 'foo' | 'bar' | 'baz';
type MyObjectType = Record<MyKeyType, number>;When composing the result from scratch, MyObjectType must be wrapped with Partial.
const result: Partial<MyObjectType> = {};
for (const key of [
'foo',
'bar',
'baz',
] as const satisfies ReadonlyArray<MyKeyType>) {
result[key] = Math.random();
}Now that we’ve determined the result, let’s revert the Partial for a clean export.
export const data = result as Required<typeof result>;
//=> const data: Required<Partial<MyObjectType>>It’s not ideal—we’d expect MyObjectType instead of the lingering type modifiers.
Here’s the same export with Complete, though:
export const data = result as Complete<typeof result>;
//=> const data: MyObjectTypeComplete improves DX the same way Simplify does. If there’s consensus around including it, I’d be happy to submit a proper PR.
Type source
type Complete<T> = T extends Partial<infer P> ? P : T;Search existing types and issues first
- I tried my best to look for it
Reactions are currently unavailable