@@ -6026,6 +6026,21 @@ namespace ts {
6026
6026
return links.type;
6027
6027
}
6028
6028
6029
+ function* getInstantiatedTypeOfInstantiatedSymbol(symbol: Symbol): Generator<[Type, TypeMapper], Type, Type> {
6030
+ const links = getSymbolLinks(symbol);
6031
+ if (!links.type) {
6032
+ if (!pushTypeResolution(symbol, TypeSystemPropertyName.Type)) {
6033
+ return links.type = errorType;
6034
+ }
6035
+ let type = yield [yield* getInstantiatedTypeOfSymbol(links.target!), links.mapper!];
6036
+ if (!popTypeResolution()) {
6037
+ type = reportCircularityError(symbol);
6038
+ }
6039
+ links.type = type;
6040
+ }
6041
+ return links.type;
6042
+ }
6043
+
6029
6044
function getTypeOfInstantiatedSymbol(symbol: Symbol): Type {
6030
6045
const links = getSymbolLinks(symbol);
6031
6046
if (!links.type) {
@@ -6070,6 +6085,16 @@ namespace ts {
6070
6085
return links.type;
6071
6086
}
6072
6087
6088
+ function* getInstantiatedTypeOfSymbol(symbol: Symbol): Generator<[Type, TypeMapper], Type, Type> {
6089
+ if (getCheckFlags(symbol) & CheckFlags.DeferredType) {
6090
+ return getTypeOfSymbolWithDeferredType(symbol);
6091
+ }
6092
+ if (getCheckFlags(symbol) & CheckFlags.Instantiated) {
6093
+ return yield* getInstantiatedTypeOfInstantiatedSymbol(symbol);
6094
+ }
6095
+ return getTypeOfSymbol(symbol);
6096
+ }
6097
+
6073
6098
function getTypeOfSymbol(symbol: Symbol): Type {
6074
6099
if (getCheckFlags(symbol) & CheckFlags.DeferredType) {
6075
6100
return getTypeOfSymbolWithDeferredType(symbol);
@@ -10258,6 +10283,45 @@ namespace ts {
10258
10283
undefined;
10259
10284
}
10260
10285
10286
+ function* getInstantiatedPropertyTypeForIndexType(objectType: Type, indexType: Type, accessFlags: AccessFlags): Generator<[Type, TypeMapper], Type | undefined, Type> {
10287
+ const propName = getPropertyNameFromIndex(indexType, /*accessNode*/ undefined);
10288
+ if (propName !== undefined) {
10289
+ const prop = getPropertyOfType(objectType, propName);
10290
+ if (prop) {
10291
+ return yield* getInstantiatedTypeOfSymbol(prop);
10292
+ }
10293
+ if (everyType(objectType, isTupleType) && isNumericLiteralName(propName) && +propName >= 0) {
10294
+ return mapType(objectType, t => getRestTypeOfTupleType(<TupleTypeReference>t) || undefinedType);
10295
+ }
10296
+ }
10297
+ if (!(indexType.flags & TypeFlags.Nullable) && isTypeAssignableToKind(indexType, TypeFlags.StringLike | TypeFlags.NumberLike | TypeFlags.ESSymbolLike)) {
10298
+ if (objectType.flags & (TypeFlags.Any | TypeFlags.Never)) {
10299
+ return objectType;
10300
+ }
10301
+ const stringIndexInfo = getIndexInfoOfType(objectType, IndexKind.String);
10302
+ const indexInfo = isTypeAssignableToKind(indexType, TypeFlags.NumberLike) && getIndexInfoOfType(objectType, IndexKind.Number) || stringIndexInfo;
10303
+ if (indexInfo) {
10304
+ if (accessFlags & AccessFlags.NoIndexSignatures && indexInfo === stringIndexInfo) {
10305
+ return undefined;
10306
+ }
10307
+ return indexInfo.type;
10308
+ }
10309
+ if (indexType.flags & TypeFlags.Never) {
10310
+ return neverType;
10311
+ }
10312
+ if (isJSLiteralType(objectType)) {
10313
+ return anyType;
10314
+ }
10315
+ }
10316
+ if (isJSLiteralType(objectType)) {
10317
+ return anyType;
10318
+ }
10319
+ if (isTypeAny(indexType)) {
10320
+ return indexType;
10321
+ }
10322
+ return undefined;
10323
+ }
10324
+
10261
10325
function getPropertyTypeForIndexType(originalObjectType: Type, objectType: Type, indexType: Type, fullIndexType: Type, suppressNoImplicitAnyError: boolean, accessNode: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression | undefined, accessFlags: AccessFlags) {
10262
10326
const accessExpression = accessNode && accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode : undefined;
10263
10327
const propName = getPropertyNameFromIndex(indexType, accessNode);
@@ -10532,7 +10596,7 @@ namespace ts {
10532
10596
return getIndexedAccessTypeOrUndefined(objectType, indexType, accessNode, AccessFlags.None) || (accessNode ? errorType : unknownType);
10533
10597
}
10534
10598
10535
- function getIndexedAccessTypeOrUndefined (objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression, accessFlags = AccessFlags.None ): Type | undefined {
10599
+ function getSimpleOrGenericIndexedAccess (objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression): Type | undefined {
10536
10600
if (objectType === wildcardType || indexType === wildcardType) {
10537
10601
return wildcardType;
10538
10602
}
@@ -10558,6 +10622,40 @@ namespace ts {
10558
10622
}
10559
10623
return type;
10560
10624
}
10625
+ }
10626
+
10627
+ function* getInstantiatedIndexedAccessType(objectType: Type, indexType: Type, accessFlags = AccessFlags.None): Generator<[Type, TypeMapper], Type | undefined, Type> {
10628
+ const simple = getSimpleOrGenericIndexedAccess(objectType, indexType);
10629
+ if (simple) {
10630
+ return simple;
10631
+ }
10632
+
10633
+ // In the following we resolve T[K] to the type of the property in T selected by K.
10634
+ // We treat boolean as different from other unions to improve errors;
10635
+ // skipping straight to getPropertyTypeForIndexType gives errors with 'boolean' instead of 'true'.
10636
+ const apparentObjectType = getApparentType(objectType);
10637
+ if (indexType.flags & TypeFlags.Union && !(indexType.flags & TypeFlags.Boolean)) {
10638
+ const propTypes: Type[] = [];
10639
+ for (const t of (<UnionType>indexType).types) {
10640
+ const propType = yield* getInstantiatedPropertyTypeForIndexType(apparentObjectType, t, accessFlags);
10641
+ if (propType) {
10642
+ propTypes.push(propType);
10643
+ }
10644
+ else {
10645
+ // If there's no error node, we can immeditely stop, since error reporting is off
10646
+ return undefined;
10647
+ }
10648
+ }
10649
+ return accessFlags & AccessFlags.Writing ? getIntersectionType(propTypes) : getUnionType(propTypes);
10650
+ }
10651
+ return yield* getInstantiatedPropertyTypeForIndexType(apparentObjectType, indexType, accessFlags | AccessFlags.CacheSymbol);
10652
+ }
10653
+
10654
+ function getIndexedAccessTypeOrUndefined(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression, accessFlags = AccessFlags.None): Type | undefined {
10655
+ const simple = getSimpleOrGenericIndexedAccess(objectType, indexType, accessNode);
10656
+ if (simple) {
10657
+ return simple;
10658
+ }
10561
10659
// In the following we resolve T[K] to the type of the property in T selected by K.
10562
10660
// We treat boolean as different from other unions to improve errors;
10563
10661
// skipping straight to getPropertyTypeForIndexType gives errors with 'boolean' instead of 'true'.
@@ -11640,7 +11738,7 @@ namespace ts {
11640
11738
return getIndexType(yield [(<IndexType>type).type, mapper]);
11641
11739
}
11642
11740
if (flags & TypeFlags.IndexedAccess) {
11643
- return getIndexedAccessType (yield [(<IndexedAccessType>type).objectType, mapper], yield [(<IndexedAccessType>type).indexType, mapper]);
11741
+ return (yield* getInstantiatedIndexedAccessType(yield [(<IndexedAccessType>type).objectType, mapper], yield [(<IndexedAccessType>type).indexType, mapper])) || unknownType ;
11644
11742
}
11645
11743
if (flags & TypeFlags.Conditional) {
11646
11744
return yield* getConditionalTypeInstantiation(<ConditionalType>type, combineTypeMappers((<ConditionalType>type).mapper, mapper));
0 commit comments