@@ -3973,7 +3973,7 @@ namespace ts {
3973
3973
return true;
3974
3974
}
3975
3975
if (type.flags & TypeFlags.TypeVariable) {
3976
- const constraint = getBaseConstraintOfType(<TypeVariable> type);
3976
+ const constraint = getBaseConstraintOfType(type);
3977
3977
return constraint && isValidBaseType(constraint) && isMixinConstructorType(constraint);
3978
3978
}
3979
3979
return false;
@@ -4989,16 +4989,32 @@ namespace ts {
4989
4989
}
4990
4990
4991
4991
function getConstraintOfType(type: TypeVariable | UnionOrIntersectionType): Type {
4992
- return type.flags & TypeFlags.TypeParameter ? getConstraintOfTypeParameter(<TypeParameter>type) : getBaseConstraintOfType(type);
4992
+ return type.flags & TypeFlags.TypeParameter ? getConstraintOfTypeParameter(<TypeParameter>type) :
4993
+ type.flags & TypeFlags.IndexedAccess ? getConstraintOfIndexedAccess(<IndexedAccessType>type) :
4994
+ getBaseConstraintOfType(type);
4993
4995
}
4994
4996
4995
4997
function getConstraintOfTypeParameter(typeParameter: TypeParameter): Type {
4996
4998
return hasNonCircularBaseConstraint(typeParameter) ? getConstraintFromTypeParameter(typeParameter) : undefined;
4997
4999
}
4998
5000
4999
- function getBaseConstraintOfType(type: TypeVariable | UnionOrIntersectionType): Type {
5000
- const constraint = getResolvedBaseConstraint(type);
5001
- return constraint !== noConstraintType && constraint !== circularConstraintType ? constraint : undefined;
5001
+ function getConstraintOfIndexedAccess(type: IndexedAccessType) {
5002
+ const baseObjectType = getBaseConstraintOfType(type.objectType);
5003
+ const baseIndexType = getBaseConstraintOfType(type.indexType);
5004
+ return baseObjectType || baseIndexType ? getIndexedAccessType(baseObjectType || type.objectType, baseIndexType || type.indexType) : undefined;
5005
+ }
5006
+
5007
+ function getBaseConstraintOfType(type: Type): Type {
5008
+ if (type.flags & (TypeFlags.TypeVariable | TypeFlags.UnionOrIntersection)) {
5009
+ const constraint = getResolvedBaseConstraint(<TypeVariable | UnionOrIntersectionType>type);
5010
+ if (constraint !== noConstraintType && constraint !== circularConstraintType) {
5011
+ return constraint;
5012
+ }
5013
+ }
5014
+ else if (type.flags & TypeFlags.Index) {
5015
+ return stringType;
5016
+ }
5017
+ return undefined;
5002
5018
}
5003
5019
5004
5020
function hasNonCircularBaseConstraint(type: TypeVariable): boolean {
@@ -5096,7 +5112,7 @@ namespace ts {
5096
5112
* type itself. Note that the apparent type of a union type is the union type itself.
5097
5113
*/
5098
5114
function getApparentType(type: Type): Type {
5099
- const t = type.flags & TypeFlags.TypeVariable ? getBaseConstraintOfType(<TypeVariable> type) || emptyObjectType : type;
5115
+ const t = type.flags & TypeFlags.TypeVariable ? getBaseConstraintOfType(type) || emptyObjectType : type;
5100
5116
return t.flags & TypeFlags.Intersection ? getApparentTypeOfIntersectionType(<IntersectionType>t) :
5101
5117
t.flags & TypeFlags.StringLike ? globalStringType :
5102
5118
t.flags & TypeFlags.NumberLike ? globalNumberType :
@@ -7921,7 +7937,7 @@ namespace ts {
7921
7937
}
7922
7938
// A type S is related to a type T[K] if S is related to A[K], where K is string-like and
7923
7939
// A is the apparent type of S.
7924
- const constraint = getBaseConstraintOfType(<IndexedAccessType> target);
7940
+ const constraint = getBaseConstraintOfType(target);
7925
7941
if (constraint) {
7926
7942
if (result = isRelatedTo(source, constraint, reportErrors)) {
7927
7943
errorInfo = saveErrorInfo;
@@ -7961,7 +7977,7 @@ namespace ts {
7961
7977
else if (source.flags & TypeFlags.IndexedAccess) {
7962
7978
// A type S[K] is related to a type T if A[K] is related to T, where K is string-like and
7963
7979
// A is the apparent type of S.
7964
- const constraint = getBaseConstraintOfType (<IndexedAccessType>source);
7980
+ const constraint = getConstraintOfType (<IndexedAccessType>source);
7965
7981
if (constraint) {
7966
7982
if (result = isRelatedTo(constraint, target, reportErrors)) {
7967
7983
errorInfo = saveErrorInfo;
@@ -9874,7 +9890,7 @@ namespace ts {
9874
9890
return strictNullChecks ? TypeFacts.ObjectStrictFacts : TypeFacts.ObjectFacts;
9875
9891
}
9876
9892
if (flags & TypeFlags.TypeVariable) {
9877
- return getTypeFacts(getBaseConstraintOfType(<TypeVariable> type) || emptyObjectType);
9893
+ return getTypeFacts(getBaseConstraintOfType(type) || emptyObjectType);
9878
9894
}
9879
9895
if (flags & TypeFlags.UnionOrIntersection) {
9880
9896
return getTypeFactsOfTypes((<UnionOrIntersectionType>type).types);
@@ -10686,7 +10702,7 @@ namespace ts {
10686
10702
return targetType;
10687
10703
}
10688
10704
if (type.flags & TypeFlags.TypeVariable) {
10689
- const constraint = getBaseConstraintOfType(<TypeVariable> type) || anyType;
10705
+ const constraint = getBaseConstraintOfType(type) || anyType;
10690
10706
if (isTypeSubtypeOf(targetType, constraint)) {
10691
10707
return getIntersectionType([type, targetType]);
10692
10708
}
@@ -16234,7 +16250,7 @@ namespace ts {
16234
16250
function isLiteralContextualType(contextualType: Type) {
16235
16251
if (contextualType) {
16236
16252
if (contextualType.flags & TypeFlags.TypeVariable) {
16237
- const constraint = getBaseConstraintOfType(<TypeVariable> contextualType) || emptyObjectType;
16253
+ const constraint = getBaseConstraintOfType(contextualType) || emptyObjectType;
16238
16254
// If the type parameter is constrained to the base primitive type we're checking for,
16239
16255
// consider this a literal context. For example, given a type parameter 'T extends string',
16240
16256
// this causes us to infer string literal types for T.
@@ -17124,7 +17140,7 @@ namespace ts {
17124
17140
// Check if we're indexing with a numeric type and the object type is a generic
17125
17141
// type with a constraint that has a numeric index signature.
17126
17142
if (maybeTypeOfKind(objectType, TypeFlags.TypeVariable) && isTypeOfKind(indexType, TypeFlags.NumberLike)) {
17127
- const constraint = getBaseConstraintOfType(<TypeVariable | UnionOrIntersectionType> objectType);
17143
+ const constraint = getBaseConstraintOfType(objectType);
17128
17144
if (constraint && getIndexInfoOfType(constraint, IndexKind.Number)) {
17129
17145
return type;
17130
17146
}
0 commit comments