Unwanted mapped type narrowing in 5.5
🔎 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.
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.
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!
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.