Skip to content

Declaration emit change in 5.5 with exactOptionalPropertyTypesΒ #59056

Closed
@samhh

Description

@samhh

πŸ”Ž Search Terms

mapped type, narrow, optional, undefined, union

πŸ•— Version & Regression Information

This changed between versions 5.4 and 5.5. This can be reproduced in the playground.

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.2#code/C4TwDgpgBAkgdhAHgQwMbAPJmASwPZzIA2AzgDwCCAfFALxQDeAUFFANoDSUOcUA1hBB4AZlApRkJKAFc4AEwjCeEOVCTAI8qRU4BdKAH4oXAFxQEANwgAnXQbOyFShKvWa52vS1aGxeqAA+MvKKynLerGY6HLoA3EwAvlAAZIzenNy8AkKi4pLBTmFqiBpafjG+ljZQZjFRevEJ8UygkLC89Myswnh49lAkwNY8AObxrABGyNZmcNIAthM241BTAF4OIc4qjUxMAPT7UACiiJDoKmYMUD19ZoPDcCOBBaEusavTswtL1h-r-Ucbx2UASLXA0Aw0mAdHaSDQmGw+EIpDI8CoTCAA

πŸ’» Code

type InexactOptionals<A> = {
  [K in keyof A as undefined extends A[K] ? K : never]?: undefined extends A[K]
    ? A[K] | undefined
    : A[K];
} & {
  [K in keyof A as undefined extends A[K] ? never : K]: A[K];
};

type In = {
  foo?: string;
  bar: number;
  baz: undefined;
}

// Expected: { foo?: string | undefined; bar: number; baz?: undefined; }
type Out = InexactOptionals<In>

πŸ™ Actual behavior

The A[K] | undefined in the mapped type ternary is narrowed to A[K], meaning that foo in the example is foo?: string.

πŸ™‚ Expected behavior

The A[K] | undefined in the mapped type ternary is not narrowed, so we see foo?: string | undefined as expected.

Additional information about the issue

My use case is cheaply migrating a codebase to support exactOptionalPropertyTypes. It can also be useful for better React DX.

Metadata

Metadata

Assignees

Labels

Domain: Declaration EmitThe issue relates to the emission of d.ts filesFix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions