@@ -9987,8 +9987,8 @@ namespace ts {
9987
9987
});
9988
9988
}
9989
9989
9990
- function getLiteralTypeFromProperty(prop: Symbol, include: TypeFlags, includeNonPublic?: boolean ) {
9991
- if (includeNonPublic || !(getDeclarationModifierFlagsFromSymbol(prop) & ModifierFlags.NonPublicAccessibilityModifier)) {
9990
+ function getLiteralTypeFromProperty(prop: Symbol, include: TypeFlags) {
9991
+ if (!(getDeclarationModifierFlagsFromSymbol(prop) & ModifierFlags.NonPublicAccessibilityModifier)) {
9992
9992
let type = getLateBoundSymbol(prop).nameType;
9993
9993
if (!type && !isKnownSymbol(prop)) {
9994
9994
if (prop.escapedName === InternalSymbolName.Default) {
@@ -10006,8 +10006,8 @@ namespace ts {
10006
10006
return neverType;
10007
10007
}
10008
10008
10009
- function getLiteralTypeFromProperties(type: Type, include: TypeFlags, includeNonPublic?: boolean ) {
10010
- return getUnionType(map(getPropertiesOfType(type), p => getLiteralTypeFromProperty(p, include, includeNonPublic )));
10009
+ function getLiteralTypeFromProperties(type: Type, include: TypeFlags) {
10010
+ return getUnionType(map(getPropertiesOfType(type), p => getLiteralTypeFromProperty(p, include)));
10011
10011
}
10012
10012
10013
10013
function getNonEnumNumberIndexInfo(type: Type) {
@@ -10023,10 +10023,10 @@ namespace ts {
10023
10023
type === wildcardType ? wildcardType :
10024
10024
type.flags & TypeFlags.Unknown ? neverType :
10025
10025
type.flags & (TypeFlags.Any | TypeFlags.Never) ? keyofConstraintType :
10026
- stringsOnly ? !noIndexSignatures && getIndexInfoOfType(type, IndexKind.String) ? stringType : getLiteralTypeFromProperties(type, TypeFlags.StringLiteral, /*includeNonPublic*/ true ) :
10027
- !noIndexSignatures && getIndexInfoOfType(type, IndexKind.String) ? getUnionType([stringType, numberType, getLiteralTypeFromProperties(type, TypeFlags.UniqueESSymbol, /*includeNonPublic*/ true )]) :
10028
- getNonEnumNumberIndexInfo(type) ? getUnionType([numberType, getLiteralTypeFromProperties(type, TypeFlags.StringLiteral | TypeFlags.UniqueESSymbol, /*includeNonPublic*/ true )]) :
10029
- getLiteralTypeFromProperties(type, TypeFlags.StringOrNumberLiteralOrUnique, /*includeNonPublic*/ true );
10026
+ stringsOnly ? !noIndexSignatures && getIndexInfoOfType(type, IndexKind.String) ? stringType : getLiteralTypeFromProperties(type, TypeFlags.StringLiteral) :
10027
+ !noIndexSignatures && getIndexInfoOfType(type, IndexKind.String) ? getUnionType([stringType, numberType, getLiteralTypeFromProperties(type, TypeFlags.UniqueESSymbol)]) :
10028
+ getNonEnumNumberIndexInfo(type) ? getUnionType([numberType, getLiteralTypeFromProperties(type, TypeFlags.StringLiteral | TypeFlags.UniqueESSymbol)]) :
10029
+ getLiteralTypeFromProperties(type, TypeFlags.StringOrNumberLiteralOrUnique);
10030
10030
}
10031
10031
10032
10032
function getExtractStringType(type: Type) {
@@ -10097,16 +10097,21 @@ namespace ts {
10097
10097
return false;
10098
10098
}
10099
10099
10100
- function getPropertyTypeForIndexType(originalObjectType : Type, objectType: Type, indexType: Type, fullIndexType: Type, suppressNoImplicitAnyError: boolean, accessNode: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression | undefined, accessFlags: AccessFlags ) {
10100
+ function getPropertyNameFromIndex(indexType : Type, accessNode: StringLiteral | Identifier | ObjectBindingPattern | ArrayBindingPattern | ComputedPropertyName | NumericLiteral | IndexedAccessTypeNode | ElementAccessExpression | SyntheticExpression | undefined) {
10101
10101
const accessExpression = accessNode && accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode : undefined;
10102
- const propName = isTypeUsableAsPropertyName(indexType) ?
10102
+ return isTypeUsableAsPropertyName(indexType) ?
10103
10103
getPropertyNameFromType(indexType) :
10104
10104
accessExpression && checkThatExpressionIsProperSymbolReference(accessExpression.argumentExpression, indexType, /*reportError*/ false) ?
10105
10105
getPropertyNameForKnownSymbolName(idText((<PropertyAccessExpression>accessExpression.argumentExpression).name)) :
10106
10106
accessNode && isPropertyName(accessNode) ?
10107
10107
// late bound names are handled in the first branch, so here we only need to handle normal names
10108
10108
getPropertyNameForPropertyNameNode(accessNode) :
10109
10109
undefined;
10110
+ }
10111
+
10112
+ function getPropertyTypeForIndexType(originalObjectType: Type, objectType: Type, indexType: Type, fullIndexType: Type, suppressNoImplicitAnyError: boolean, accessNode: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression | undefined, accessFlags: AccessFlags) {
10113
+ const accessExpression = accessNode && accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode : undefined;
10114
+ const propName = getPropertyNameFromIndex(indexType, accessNode);
10110
10115
if (propName !== undefined) {
10111
10116
const prop = getPropertyOfType(objectType, propName);
10112
10117
if (prop) {
@@ -25292,7 +25297,7 @@ namespace ts {
25292
25297
forEach(node.types, checkSourceElement);
25293
25298
}
25294
25299
25295
- function checkIndexedAccessIndexType(type: Type, accessNode: Node ) {
25300
+ function checkIndexedAccessIndexType(type: Type, accessNode: IndexedAccessTypeNode | ElementAccessExpression ) {
25296
25301
if (!(type.flags & TypeFlags.IndexedAccess)) {
25297
25302
return type;
25298
25303
}
@@ -25308,9 +25313,20 @@ namespace ts {
25308
25313
}
25309
25314
// Check if we're indexing with a numeric type and if either object or index types
25310
25315
// is a generic type with a constraint that has a numeric index signature.
25311
- if (getIndexInfoOfType(getApparentType(objectType), IndexKind.Number) && isTypeAssignableToKind(indexType, TypeFlags.NumberLike)) {
25316
+ const apparentObjectType = getApparentType(objectType);
25317
+ if (getIndexInfoOfType(apparentObjectType, IndexKind.Number) && isTypeAssignableToKind(indexType, TypeFlags.NumberLike)) {
25312
25318
return type;
25313
25319
}
25320
+ if (isGenericObjectType(objectType)) {
25321
+ const propertyName = getPropertyNameFromIndex(indexType, accessNode);
25322
+ if (propertyName) {
25323
+ const propertySymbol = forEachType(apparentObjectType, t => getPropertyOfType(t, propertyName));
25324
+ if (propertySymbol && getDeclarationModifierFlagsFromSymbol(propertySymbol) & ModifierFlags.NonPublicAccessibilityModifier) {
25325
+ error(accessNode, Diagnostics.Private_or_protected_member_0_cannot_be_accessed_on_a_type_parameter, unescapeLeadingUnderscores(propertyName));
25326
+ return errorType;
25327
+ }
25328
+ }
25329
+ }
25314
25330
error(accessNode, Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(indexType), typeToString(objectType));
25315
25331
return errorType;
25316
25332
}
0 commit comments