Skip to content

Consider UnwrapPartial<T> as a way to revert the Partial<T> modifier #1294

@porada

Description

@porada

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: MyObjectType

Complete 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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions