TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Unwanted mapped type narrowing in 5.5

Open samhh opened this issue 1 year ago • 3 comments

🔎 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.

samhh avatar Jun 27 '24 17:06 samhh

Bisects to https://github.com/microsoft/TypeScript/pull/57995 . It's unclear what you are asking for here. This is purely a display change, not a functional change.

Andarist avatar Jun 28 '24 07:06 Andarist

Oh snap, it is just a display change as per:

// ✅
const out: Out = {
  foo: undefined,
  bar: 123,
}

I saw a new type error in 5.5 and the display change made me assume that this is what it was. Thanks for taking a look!

samhh avatar Jun 28 '24 08:06 samhh

I think there might actually still be an issue here, see the declaration output: https://www.typescriptlang.org/play/?ts=5.5.2#code/C4TwDgpgBAkgdhAHgQwMbAPJmASwPZzIA2AzgDwCCAfFALxQDeAUFFANoDSUOcUA1hBB4AZlApRkJKAFc4AEwjCeEOVCTAI8qRU4BdKAH4oXAFxQEANwgAnXQbOyFShKvWa52vS1aGxeqAA+MvKKynLerGY6HLoA3EwAvlAAZIzenNy8AkKi4pLBTmFqiBpafjG+ljZQZjFRevEJ8UygkLC89Myswnh49lAkwNY8AObxrABGyNZmcNIAthM241BTAF4OIc4qjUwt4NAY0sB07UhomNj4hKRk8FR7qASDUD14p5SnDAlUABQAlHQaL9EGYjidUhRAbQaHMiEQ9kgwHhrCcnnAXlM5HJgFJ6G8AUwgA

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;
}

type Out = InexactOptionals<In>

const foo = <A = {}>() => (x: Out & A) => null

export const baddts = foo()

5.4:

export declare const baddts: (x: {
    foo?: string | undefined;
    baz?: undefined;
} & {
    bar: number;
}) => null;

5.5:

export declare const baddts: (x: {
    foo?: string;
    baz?: undefined;
} & {
    bar: number;
}) => null;

This in turn causes problems for consumers with exactOptionalPropertyTypes.

samhh avatar Jun 28 '24 13:06 samhh