Skip to content

Fixed an issue with optionality not being stripped from generic mapped types on indexed access #59921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19103,9 +19103,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const mapper = createTypeMapper([getTypeParameterFromMappedType(objectType)], [index]);
const templateMapper = combineTypeMappers(objectType.mapper, mapper);
const instantiatedTemplateType = instantiateType(getTemplateTypeFromMappedType(objectType.target as MappedType || objectType), templateMapper);
const isOptional = getMappedTypeOptionality(objectType) > 0 || (isGenericType(objectType) ?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the given repro the immediate modifiers type has -? for which getMappedTypeOptionality returns -1. So the first condition was not satisfied and the compiler proceeded to compute getCombinedMappedTypeOptionality. This time it was called with the modifiers type and not the original object type. Since that was a mapped type too it got its modifiers type etc.

Essentially this was skipping the outermost mapped type modifiers in this scenario - the ones that were supposed strip the optionality.

getCombinedMappedTypeOptionality(getModifiersTypeFromMappedType(objectType)) > 0 :
couldAccessOptionalProperty(objectType, index));
const isOptional = isGenericType(objectType) ?
getCombinedMappedTypeOptionality(objectType) > 0 :
getMappedTypeOptionality(objectType) > 0 || couldAccessOptionalProperty(objectType, index);
return addOptionality(instantiatedTemplateType, /*isProperty*/ true, isOptional);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
genericMappedTypeOptionalityModifier1.ts(10,9): error TS2322: Type 'undefined' is not assignable to type 'RequiredValues'.


==== genericMappedTypeOptionalityModifier1.ts (1 errors) ====
// https://github.com/microsoft/TypeScript/issues/59902

const example = <T extends string>() => {
type SomeOptional = { [key in T]?: 1 };

type AllRequired = { [key in keyof SomeOptional]-?: 1 };

type RequiredValues = AllRequired[keyof AllRequired];

const x: RequiredValues = undefined; // error
~
!!! error TS2322: Type 'undefined' is not assignable to type 'RequiredValues'.

return x;
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [tests/cases/compiler/genericMappedTypeOptionalityModifier1.ts] ////

=== genericMappedTypeOptionalityModifier1.ts ===
// https://github.com/microsoft/TypeScript/issues/59902

const example = <T extends string>() => {
>example : Symbol(example, Decl(genericMappedTypeOptionalityModifier1.ts, 2, 5))
>T : Symbol(T, Decl(genericMappedTypeOptionalityModifier1.ts, 2, 17))

type SomeOptional = { [key in T]?: 1 };
>SomeOptional : Symbol(SomeOptional, Decl(genericMappedTypeOptionalityModifier1.ts, 2, 41))
>key : Symbol(key, Decl(genericMappedTypeOptionalityModifier1.ts, 3, 25))
>T : Symbol(T, Decl(genericMappedTypeOptionalityModifier1.ts, 2, 17))

type AllRequired = { [key in keyof SomeOptional]-?: 1 };
>AllRequired : Symbol(AllRequired, Decl(genericMappedTypeOptionalityModifier1.ts, 3, 41))
>key : Symbol(key, Decl(genericMappedTypeOptionalityModifier1.ts, 5, 24))
>SomeOptional : Symbol(SomeOptional, Decl(genericMappedTypeOptionalityModifier1.ts, 2, 41))

type RequiredValues = AllRequired[keyof AllRequired];
>RequiredValues : Symbol(RequiredValues, Decl(genericMappedTypeOptionalityModifier1.ts, 5, 58))
>AllRequired : Symbol(AllRequired, Decl(genericMappedTypeOptionalityModifier1.ts, 3, 41))
>AllRequired : Symbol(AllRequired, Decl(genericMappedTypeOptionalityModifier1.ts, 3, 41))

const x: RequiredValues = undefined; // error
>x : Symbol(x, Decl(genericMappedTypeOptionalityModifier1.ts, 9, 7))
>RequiredValues : Symbol(RequiredValues, Decl(genericMappedTypeOptionalityModifier1.ts, 5, 58))
>undefined : Symbol(undefined)

return x;
>x : Symbol(x, Decl(genericMappedTypeOptionalityModifier1.ts, 9, 7))

};

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//// [tests/cases/compiler/genericMappedTypeOptionalityModifier1.ts] ////

=== genericMappedTypeOptionalityModifier1.ts ===
// https://github.com/microsoft/TypeScript/issues/59902

const example = <T extends string>() => {
>example : <T extends string>() => { [key_1 in keyof { [key in T]?: 1 | undefined; }]-?: 1; }[T]
> : ^ ^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
><T extends string>() => { type SomeOptional = { [key in T]?: 1 }; type AllRequired = { [key in keyof SomeOptional]-?: 1 }; type RequiredValues = AllRequired[keyof AllRequired]; const x: RequiredValues = undefined; // error return x;} : <T extends string>() => { [key_1 in keyof { [key in T]?: 1 | undefined; }]-?: 1; }[T]
> : ^ ^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

type SomeOptional = { [key in T]?: 1 };
>SomeOptional : { [key in T]?: 1 | undefined; }
> : ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^

type AllRequired = { [key in keyof SomeOptional]-?: 1 };
>AllRequired : { [key_1 in keyof { [key in T]?: 1 | undefined; }]-?: 1; }
> : ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

type RequiredValues = AllRequired[keyof AllRequired];
>RequiredValues : { [key_1 in keyof { [key in T]?: 1 | undefined; }]-?: 1; }[T]
> : ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

const x: RequiredValues = undefined; // error
>x : { [key_1 in keyof { [key in T]?: 1 | undefined; }]-?: 1; }[T]
> : ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>undefined : undefined
> : ^^^^^^^^^

return x;
>x : { [key_1 in keyof { [key in T]?: 1 | undefined; }]-?: 1; }[T]
> : ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

};

16 changes: 16 additions & 0 deletions tests/cases/compiler/genericMappedTypeOptionalityModifier1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @strict: true
// @noEmit: true

// https://github.com/microsoft/TypeScript/issues/59902

const example = <T extends string>() => {
type SomeOptional = { [key in T]?: 1 };

type AllRequired = { [key in keyof SomeOptional]-?: 1 };

type RequiredValues = AllRequired[keyof AllRequired];

const x: RequiredValues = undefined; // error

return x;
};