Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 15 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,12 @@ const enum MappedTypeModifiers {
ExcludeOptional = 1 << 3,
}

const enum MappedTypeNameTypeKind {
None = 0,
Filtering = 1 << 0,
Remapping = 1 << 1,
Copy link
Member

Choose a reason for hiding this comment

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

Why is this a flags enum? I can't see anywhere we actually combine multiple flags - in fact, it seems to me like a mapped type can only be one, given the implementation of getMappedTypeNameTypeKind.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had some composite values here before but removed them at the end since I didn't actually use them anywhere. Refactored this now to be a non-flag enum

}

const enum ExpandingFlags {
None = 0,
Source = 1,
Expand Down Expand Up @@ -13224,7 +13230,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const constraintType = getConstraintTypeFromMappedType(type);
const mappedType = (type.target as MappedType) || type;
const nameType = getNameTypeFromMappedType(mappedType);
const shouldLinkPropDeclarations = !nameType || isFilteringMappedType(mappedType);
const shouldLinkPropDeclarations = !(getMappedTypeNameTypeKind(mappedType) & MappedTypeNameTypeKind.Remapping);
const templateType = getTemplateTypeFromMappedType(mappedType);
const modifiersType = getApparentType(getModifiersTypeFromMappedType(type)); // The 'T' in 'keyof T'
const templateModifiers = getMappedTypeModifiers(type);
Expand Down Expand Up @@ -13404,9 +13410,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return false;
}

function isFilteringMappedType(type: MappedType): boolean {
function getMappedTypeNameTypeKind(type: MappedType): MappedTypeNameTypeKind {
const nameType = getNameTypeFromMappedType(type);
return !!nameType && isTypeAssignableTo(nameType, getTypeParameterFromMappedType(type));
if (!nameType) {
return MappedTypeNameTypeKind.None;
}
return isTypeAssignableTo(nameType, getTypeParameterFromMappedType(type)) ? MappedTypeNameTypeKind.Filtering : MappedTypeNameTypeKind.Remapping;
}

function resolveStructuredTypeMembers(type: StructuredType): ResolvedType {
Expand Down Expand Up @@ -17075,7 +17084,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function shouldDeferIndexType(type: Type, indexFlags = IndexFlags.None) {
return !!(type.flags & TypeFlags.InstantiableNonPrimitive ||
isGenericTupleType(type) ||
isGenericMappedType(type) && !hasDistributiveNameType(type) ||
isGenericMappedType(type) && (!hasDistributiveNameType(type) || !(indexFlags & IndexFlags.NoRemappingMappedTypeDeferral) && getMappedTypeNameTypeKind(type) & MappedTypeNameTypeKind.Remapping) ||
type.flags & TypeFlags.Union && !(indexFlags & IndexFlags.NoReducibleCheck) && isGenericReducibleType(type) ||
type.flags & TypeFlags.Intersection && maybeTypeOfKind(type, TypeFlags.Instantiable) && some((type as IntersectionType).types, isEmptyAnonymousObjectType));
}
Expand Down Expand Up @@ -17632,7 +17641,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// K is generic and N is assignable to P, instantiate E using a mapper that substitutes the index type for P.
// 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 (!(getMappedTypeNameTypeKind(objectType) & MappedTypeNameTypeKind.Remapping)) {
return type[cache] = mapType(substituteIndexedMappedType(objectType, type.indexType), t => getSimplifiedType(t, writing));
}
}
Expand Down Expand Up @@ -38913,7 +38922,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// Check if the index type is assignable to 'keyof T' for the object type.
const objectType = (type as IndexedAccessType).objectType;
const indexType = (type as IndexedAccessType).indexType;
if (isTypeAssignableTo(indexType, getIndexType(objectType, IndexFlags.None))) {
if (isTypeAssignableTo(indexType, getIndexType(objectType, IndexFlags.NoRemappingMappedTypeDeferral))) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I kinda expected that I would have to pass this at more locations. Given the fact that this is the only call site using this... maybe I just should remove this IndexFlags.NoRemappingMappedTypeDeferral and perform the targeted simplification of keyof RemappingMappedType here to pass it to the isTypeAssignableTo.

Copy link
Member

Choose a reason for hiding this comment

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

That would probably be better - IndexFlags originally only existed to support the keyofStringsOnly flag, and its' other uses have kind of become a convenience. With keyofStringsOnly likely to go away soonish, it'd be nice if we could stop having all these quiet variant index types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pushed out this change

if (accessNode.kind === SyntaxKind.ElementAccessExpression && isAssignmentTarget(accessNode) &&
getObjectFlags(objectType) & ObjectFlags.Mapped && getMappedTypeModifiers(objectType as MappedType) & MappedTypeModifiers.IncludeReadonly) {
error(accessNode, Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(objectType));
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6634,6 +6634,7 @@ export const enum IndexFlags {
StringsOnly = 1 << 0,
NoIndexSignatures = 1 << 1,
NoReducibleCheck = 1 << 2,
NoRemappingMappedTypeDeferral = 1 << 3,
}

// keyof T types (TypeFlags.Index)
Expand Down
28 changes: 26 additions & 2 deletions tests/baselines/reference/mappedTypeAsClauses.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mappedTypeAsClauses.ts(130,3): error TS2345: Argument of type '"a"' is not assignable to parameter of type '"b"'.
mappedTypeAsClauses.ts(131,3): error TS2345: Argument of type '"a"' is not assignable to parameter of type '"b"'.


==== mappedTypeAsClauses.ts (1 errors) ====
Expand Down Expand Up @@ -30,7 +30,8 @@ mappedTypeAsClauses.ts(130,3): error TS2345: Argument of type '"a"' is not assig
type DoubleProp<T> = { [P in keyof T & string as `${P}1` | `${P}2`]: T[P] }
type TD1 = DoubleProp<{ a: string, b: number }>; // { a1: string, a2: string, b1: number, b2: number }
type TD2 = keyof TD1; // 'a1' | 'a2' | 'b1' | 'b2'
type TD3<U> = keyof DoubleProp<U>; // `${keyof U & string}1` | `${keyof U & string}2`
type TD3<U> = keyof DoubleProp<U>; // keyof DoubleProp<U>
type TD4 = TD3<{ a: string, b: number }>; // 'a1' | 'a2' | 'b1' | 'b2'

// Repro from #40619

Expand Down Expand Up @@ -155,4 +156,27 @@ mappedTypeAsClauses.ts(130,3): error TS2345: Argument of type '"a"' is not assig
type TN3<T> = keyof { [P in keyof T as Exclude<Exclude<Exclude<P, 'c'>, 'b'>, 'a'>]: string };
type TN4<T, U> = keyof { [K in keyof T as (K extends U ? T[K] : never) extends T[K] ? K : never]: string };
type TN5<T, U> = keyof { [K in keyof T as keyof { [P in K as T[P] extends U ? K : never]: true }]: string };

// repro from https://github.com/microsoft/TypeScript/issues/55129
type Fruit =
| {
name: "apple";
color: "red";
}
| {
name: "banana";
color: "yellow";
}
| {
name: "orange";
color: "orange";
};
type Result1<T extends {name: string | number; color: string | number }> = {
[Key in T as `${Key['name']}:${Key['color']}`]: unknown
};
type Result2<T extends {name: string | number; color: string | number }> = keyof {
[Key in T as `${Key['name']}:${Key['color']}`]: unknown
}
type Test1 = keyof Result1<Fruit> // "apple:red" | "banana:yellow" | "orange:orange"
type Test2 = Result2<Fruit> // "apple:red" | "banana:yellow" | "orange:orange"

54 changes: 53 additions & 1 deletion tests/baselines/reference/mappedTypeAsClauses.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type TM1 = Methods<{ foo(): number, bar(x: string): boolean, baz: string | numbe
type DoubleProp<T> = { [P in keyof T & string as `${P}1` | `${P}2`]: T[P] }
type TD1 = DoubleProp<{ a: string, b: number }>; // { a1: string, a2: string, b1: number, b2: number }
type TD2 = keyof TD1; // 'a1' | 'a2' | 'b1' | 'b2'
type TD3<U> = keyof DoubleProp<U>; // `${keyof U & string}1` | `${keyof U & string}2`
type TD3<U> = keyof DoubleProp<U>; // keyof DoubleProp<U>
type TD4 = TD3<{ a: string, b: number }>; // 'a1' | 'a2' | 'b1' | 'b2'

// Repro from #40619

Expand Down Expand Up @@ -152,6 +153,29 @@ type TN2<T> = keyof { [P in keyof T as 'a' extends P ? 'x' : 'y']: string };
type TN3<T> = keyof { [P in keyof T as Exclude<Exclude<Exclude<P, 'c'>, 'b'>, 'a'>]: string };
type TN4<T, U> = keyof { [K in keyof T as (K extends U ? T[K] : never) extends T[K] ? K : never]: string };
type TN5<T, U> = keyof { [K in keyof T as keyof { [P in K as T[P] extends U ? K : never]: true }]: string };

// repro from https://github.com/microsoft/TypeScript/issues/55129
type Fruit =
| {
name: "apple";
color: "red";
}
| {
name: "banana";
color: "yellow";
}
| {
name: "orange";
color: "orange";
};
type Result1<T extends {name: string | number; color: string | number }> = {
[Key in T as `${Key['name']}:${Key['color']}`]: unknown
};
type Result2<T extends {name: string | number; color: string | number }> = keyof {
[Key in T as `${Key['name']}:${Key['color']}`]: unknown
}
type Test1 = keyof Result1<Fruit> // "apple:red" | "banana:yellow" | "orange:orange"
type Test2 = Result2<Fruit> // "apple:red" | "banana:yellow" | "orange:orange"


//// [mappedTypeAsClauses.js]
Expand Down Expand Up @@ -217,6 +241,10 @@ type TD1 = DoubleProp<{
}>;
type TD2 = keyof TD1;
type TD3<U> = keyof DoubleProp<U>;
type TD4 = TD3<{
a: string;
b: number;
}>;
type Lazyify<T> = {
[K in keyof T as `get${Capitalize<K & string>}`]: () => T[K];
};
Expand Down Expand Up @@ -337,3 +365,27 @@ type TN5<T, U> = keyof {
[P in K as T[P] extends U ? K : never]: true;
}]: string;
};
type Fruit = {
name: "apple";
color: "red";
} | {
name: "banana";
color: "yellow";
} | {
name: "orange";
color: "orange";
};
type Result1<T extends {
name: string | number;
color: string | number;
}> = {
[Key in T as `${Key['name']}:${Key['color']}`]: unknown;
};
type Result2<T extends {
name: string | number;
color: string | number;
}> = keyof {
[Key in T as `${Key['name']}:${Key['color']}`]: unknown;
};
type Test1 = keyof Result1<Fruit>;
type Test2 = Result2<Fruit>;
Loading