Skip to content

Commit 940e1ad

Browse files
committed
Bring back resolving apparent type for mapped types with tuple constraints but avoid resolving the apparent type of mapped types for contextual types
1 parent 29d4bb9 commit 940e1ad

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13861,7 +13861,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1386113861
const typeVariable = getHomomorphicTypeVariable(type);
1386213862
if (typeVariable && !type.declaration.nameType) {
1386313863
const constraint = getConstraintOfTypeParameter(typeVariable);
13864-
if (constraint && everyType(constraint, isArrayType)) {
13864+
if (constraint && everyType(constraint, isArrayOrTupleType)) {
1386513865
return instantiateType(type, prependTypeMapping(typeVariable, constraint, type.mapper));
1386613866
}
1386713867
}
@@ -29205,7 +29205,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2920529205
getContextualType(node, contextFlags);
2920629206
const instantiatedType = instantiateContextualType(contextualType, node, contextFlags);
2920729207
if (instantiatedType && !(contextFlags && contextFlags & ContextFlags.NoConstraints && instantiatedType.flags & TypeFlags.TypeVariable)) {
29208-
const apparentType = mapType(instantiatedType, getApparentType, /*noReductions*/ true);
29208+
const apparentType = mapType(
29209+
instantiatedType,
29210+
// When obtaining apparent type of *contextual* type we don't want to get apparent type of mapped types.
29211+
// That would evaluate mapped types with array or tuple type constraints too eagerly
29212+
// and thus it would prevent `getTypeOfPropertyOfContextualType` from obtaining per-position contextual type for elements of array literal expressions.
29213+
// Apparent type of other mapped types is already the mapped type itself so we can just avoid calling `getApparentType` here for all mapped types.
29214+
t => getObjectFlags(t) & ObjectFlags.Mapped ? t : getApparentType(t),
29215+
/*noReductions*/ true
29216+
);
2920929217
return apparentType.flags & TypeFlags.Union && isObjectLiteralExpression(node) ? discriminateContextualTypeByObjectMembers(node, apparentType as UnionType) :
2921029218
apparentType.flags & TypeFlags.Union && isJsxAttributes(node) ? discriminateContextualTypeByJSXAttributes(node, apparentType as UnionType) :
2921129219
apparentType;

tests/baselines/reference/mappedTypeUnionConstrainTupleTreatedAsArrayLike.types

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
=== tests/cases/compiler/mappedTypeUnionConstrainTupleTreatedAsArrayLike.ts ===
22
type HomomorphicMappedType<T> = { [P in keyof T]: T[P] extends string ? boolean : null }
33
>HomomorphicMappedType : HomomorphicMappedType<T>
4-
>null : null
54

65
function test1<T extends [number] | [string]>(args: T) {
76
>test1 : <T extends [number] | [string]>(args: T) => void

tests/baselines/reference/restParamUsingMappedTypeOverUnionConstraint.types

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
type HomomorphicMappedType<T> = { [P in keyof T]: T[P] extends string ? boolean : null }
55
>HomomorphicMappedType : HomomorphicMappedType<T>
6-
>null : null
76

87
declare function test<T extends [number] | [string]>(
98
>test : <T extends [number] | [string]>(args: T, fn: (...args: HomomorphicMappedType<T>) => void) => void

0 commit comments

Comments
 (0)