Skip to content

Distribute index over mapped types when normalizing types #55130

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 2 commits 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
17 changes: 12 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17870,8 +17870,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
(type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.Index | TypeFlags.StringMapping) && !isPatternLiteralType(type) ? ObjectFlags.IsGenericIndexType : 0);
}

function getSimplifiedType(type: Type, writing: boolean): Type {
return type.flags & TypeFlags.IndexedAccess ? getSimplifiedIndexedAccessType(type as IndexedAccessType, writing) :
function getSimplifiedType(type: Type, writing: boolean, distributeIndexOverMappedType = false): Type {
return type.flags & TypeFlags.IndexedAccess ? getSimplifiedIndexedAccessType(type as IndexedAccessType, writing, distributeIndexOverMappedType) :
type.flags & TypeFlags.Conditional ? getSimplifiedConditionalType(type as ConditionalType, writing) :
type;
}
Expand All @@ -17898,8 +17898,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// Transform an indexed access to a simpler form, if possible. Return the simpler form, or return
// the type itself if no transformation is possible. The writing flag indicates that the type is
// the target of an assignment.
function getSimplifiedIndexedAccessType(type: IndexedAccessType, writing: boolean): Type {
const cache = writing ? "simplifiedForWriting" : "simplifiedForReading";
function getSimplifiedIndexedAccessType(type: IndexedAccessType, writing: boolean, distributeIndexOverMappedType: boolean): Type {
const cache = writing ? "simplifiedForWriting" : `simplifiedForReading${distributeIndexOverMappedType ? "D" : ""}` as const;
if (type[cache]) {
return type[cache] === circularConstraintType ? type : type[cache]!;
}
Expand Down Expand Up @@ -17941,6 +17941,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// For example, for an index access { [P in K]: Box<T[P]> }[X], we construct the type Box<T[X]>.
if (isGenericMappedType(objectType)) {
if (!getNameTypeFromMappedType(objectType) || isFilteringMappedType(objectType)) {
if (distributeIndexOverMappedType && !writing) {
const indexConstraint = indexType.flags & TypeFlags.Instantiable && getBaseConstraintOfType(indexType);
if (indexConstraint && indexConstraint.flags & TypeFlags.Union) {
return type[cache] = getSimplifiedType(getIndexedAccessType(objectType, getIntersectionType([indexType, indexConstraint])), writing);
}
}

return type[cache] = mapType(substituteIndexedMappedType(objectType, type.indexType), t => getSimplifiedType(t, writing));
}
}
Expand Down Expand Up @@ -20593,7 +20600,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
getObjectFlags(type) & ObjectFlags.Reference ? (type as TypeReference).node ? createTypeReference((type as TypeReference).target, getTypeArguments(type as TypeReference)) : getSingleBaseForNonAugmentingSubtype(type) || type :
type.flags & TypeFlags.UnionOrIntersection ? getNormalizedUnionOrIntersectionType(type as UnionOrIntersectionType, writing) :
type.flags & TypeFlags.Substitution ? writing ? (type as SubstitutionType).baseType : getSubstitutionIntersection(type as SubstitutionType) :
type.flags & TypeFlags.Simplifiable ? getSimplifiedType(type, writing) :
type.flags & TypeFlags.Simplifiable ? getSimplifiedType(type, writing, /*distributeIndexOverMappedType*/ true) :
type;
if (t === type) return t;
type = t;
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6555,6 +6555,7 @@ export interface IndexedAccessType extends InstantiableType {
constraint?: Type;
simplifiedForReading?: Type;
simplifiedForWriting?: Type;
simplifiedForReadingD?: Type;
Copy link
Member

@weswigham weswigham Sep 13, 2023

Choose a reason for hiding this comment

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

Maybe D -> Distributive here, if this gets kept.

}

export type TypeVariable = TypeParameter | IndexedAccessType;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7209,6 +7209,7 @@ declare namespace ts {
constraint?: Type;
simplifiedForReading?: Type;
simplifiedForWriting?: Type;
simplifiedForReadingD?: Type;
}
type TypeVariable = TypeParameter | IndexedAccessType;
interface IndexType extends InstantiableType {
Expand Down
60 changes: 26 additions & 34 deletions tests/baselines/reference/conditionalTypes1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ conditionalTypes1.ts(17,5): error TS2322: Type 'T' is not assignable to type 'No
Type 'T' is not assignable to type '{}'.
Type 'string | undefined' is not assignable to type '{}'.
Type 'undefined' is not assignable to type '{}'.
conditionalTypes1.ts(24,5): error TS2322: Type 'T[keyof T] | undefined' is not assignable to type 'NonNullable<Partial<T>[keyof T]>'.
conditionalTypes1.ts(24,5): error TS2322: Type 'T[keyof T & string] | T[keyof T & number] | T[keyof T & symbol] | undefined' is not assignable to type 'NonNullable<Partial<T>[keyof T]>'.
Type 'undefined' is not assignable to type 'T[keyof T] & {}'.
Type 'undefined' is not assignable to type 'T[keyof T]'.
'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
Expand All @@ -25,32 +25,28 @@ conditionalTypes1.ts(104,5): error TS2322: Type 'NonFunctionProperties<T>' is no
'T' could be instantiated with an arbitrary type which could be unrelated to 'NonFunctionProperties<T>'.
conditionalTypes1.ts(106,5): error TS2322: Type 'NonFunctionProperties<T>' is not assignable to type 'FunctionProperties<T>'.
Type 'FunctionPropertyNames<T>' is not assignable to type 'NonFunctionPropertyNames<T>'.
Type 'keyof T' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
Type 'string | number | symbol' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
Type 'string' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
Type 'keyof T' is not assignable to type 'never'.
Type 'string | number | symbol' is not assignable to type 'never'.
Type 'string' is not assignable to type 'never'.
Type 'T[keyof T & string] extends Function ? keyof T & string : never' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
Type 'keyof T & string' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
Type 'keyof T & string' is not assignable to type 'never'.
conditionalTypes1.ts(108,5): error TS2322: Type 'FunctionProperties<T>' is not assignable to type 'NonFunctionProperties<T>'.
Type 'NonFunctionPropertyNames<T>' is not assignable to type 'FunctionPropertyNames<T>'.
Type 'keyof T' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
Type 'string | number | symbol' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
Type 'string' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
Type 'keyof T' is not assignable to type 'never'.
Type 'T[keyof T & string] extends Function ? never : keyof T & string' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
Type 'keyof T & string' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
Type 'keyof T & string' is not assignable to type 'never'.
conditionalTypes1.ts(114,5): error TS2322: Type 'keyof T' is not assignable to type 'FunctionPropertyNames<T>'.
Type 'string | number | symbol' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
Type 'string' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
conditionalTypes1.ts(115,5): error TS2322: Type 'NonFunctionPropertyNames<T>' is not assignable to type 'FunctionPropertyNames<T>'.
Type 'keyof T' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
Type 'keyof T' is not assignable to type 'never'.
Type 'string | number | symbol' is not assignable to type 'never'.
Type 'string' is not assignable to type 'never'.
Type 'T[keyof T & string] extends Function ? never : keyof T & string' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
Type 'keyof T & string' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
Type 'keyof T & string' is not assignable to type 'never'.
conditionalTypes1.ts(116,5): error TS2322: Type 'keyof T' is not assignable to type 'NonFunctionPropertyNames<T>'.
Type 'string | number | symbol' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
Type 'string' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
conditionalTypes1.ts(117,5): error TS2322: Type 'FunctionPropertyNames<T>' is not assignable to type 'NonFunctionPropertyNames<T>'.
Type 'keyof T' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
Type 'keyof T' is not assignable to type 'never'.
Type 'T[keyof T & string] extends Function ? keyof T & string : never' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
Type 'keyof T & string' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
Type 'keyof T & string' is not assignable to type 'never'.
conditionalTypes1.ts(134,10): error TS2540: Cannot assign to 'id' because it is a read-only property.
conditionalTypes1.ts(135,5): error TS2542: Index signature in type 'DeepReadonlyArray<Part>' only permits reading.
conditionalTypes1.ts(136,22): error TS2540: Cannot assign to 'id' because it is a read-only property.
Expand Down Expand Up @@ -108,7 +104,7 @@ conditionalTypes1.ts(288,43): error TS2322: Type 'T95<U>' is not assignable to t
x = y;
y = x; // Error
~
!!! error TS2322: Type 'T[keyof T] | undefined' is not assignable to type 'NonNullable<Partial<T>[keyof T]>'.
!!! error TS2322: Type 'T[keyof T & string] | T[keyof T & number] | T[keyof T & symbol] | undefined' is not assignable to type 'NonNullable<Partial<T>[keyof T]>'.
!!! error TS2322: Type 'undefined' is not assignable to type 'T[keyof T] & {}'.
!!! error TS2322: Type 'undefined' is not assignable to type 'T[keyof T]'.
!!! error TS2322: 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
Expand Down Expand Up @@ -211,21 +207,17 @@ conditionalTypes1.ts(288,43): error TS2322: Type 'T95<U>' is not assignable to t
~
!!! error TS2322: Type 'NonFunctionProperties<T>' is not assignable to type 'FunctionProperties<T>'.
!!! error TS2322: Type 'FunctionPropertyNames<T>' is not assignable to type 'NonFunctionPropertyNames<T>'.
!!! error TS2322: Type 'keyof T' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
!!! error TS2322: Type 'string' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
!!! error TS2322: Type 'keyof T' is not assignable to type 'never'.
!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'never'.
!!! error TS2322: Type 'string' is not assignable to type 'never'.
!!! error TS2322: Type 'T[keyof T & string] extends Function ? keyof T & string : never' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
!!! error TS2322: Type 'keyof T & string' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
!!! error TS2322: Type 'keyof T & string' is not assignable to type 'never'.
z = x;
z = y; // Error
~
!!! error TS2322: Type 'FunctionProperties<T>' is not assignable to type 'NonFunctionProperties<T>'.
!!! error TS2322: Type 'NonFunctionPropertyNames<T>' is not assignable to type 'FunctionPropertyNames<T>'.
!!! error TS2322: Type 'keyof T' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
!!! error TS2322: Type 'string' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
!!! error TS2322: Type 'keyof T' is not assignable to type 'never'.
!!! error TS2322: Type 'T[keyof T & string] extends Function ? never : keyof T & string' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
!!! error TS2322: Type 'keyof T & string' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
!!! error TS2322: Type 'keyof T & string' is not assignable to type 'never'.
}

function f8<T>(x: keyof T, y: FunctionPropertyNames<T>, z: NonFunctionPropertyNames<T>) {
Expand All @@ -239,10 +231,9 @@ conditionalTypes1.ts(288,43): error TS2322: Type 'T95<U>' is not assignable to t
y = z; // Error
~
!!! error TS2322: Type 'NonFunctionPropertyNames<T>' is not assignable to type 'FunctionPropertyNames<T>'.
!!! error TS2322: Type 'keyof T' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
!!! error TS2322: Type 'keyof T' is not assignable to type 'never'.
!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'never'.
!!! error TS2322: Type 'string' is not assignable to type 'never'.
!!! error TS2322: Type 'T[keyof T & string] extends Function ? never : keyof T & string' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
!!! error TS2322: Type 'keyof T & string' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
!!! error TS2322: Type 'keyof T & string' is not assignable to type 'never'.
z = x; // Error
~
!!! error TS2322: Type 'keyof T' is not assignable to type 'NonFunctionPropertyNames<T>'.
Expand All @@ -251,8 +242,9 @@ conditionalTypes1.ts(288,43): error TS2322: Type 'T95<U>' is not assignable to t
z = y; // Error
~
!!! error TS2322: Type 'FunctionPropertyNames<T>' is not assignable to type 'NonFunctionPropertyNames<T>'.
!!! error TS2322: Type 'keyof T' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
!!! error TS2322: Type 'keyof T' is not assignable to type 'never'.
!!! error TS2322: Type 'T[keyof T & string] extends Function ? keyof T & string : never' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
!!! error TS2322: Type 'keyof T & string' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
!!! error TS2322: Type 'keyof T & string' is not assignable to type 'never'.
}

type DeepReadonly<T> =
Expand Down
Loading