@@ -4635,6 +4635,10 @@ namespace ts {
4635
4635
if (isTypeAny(parentType)) {
4636
4636
return parentType;
4637
4637
}
4638
+ // Relax null check on ambient destructuring parameters, since the parameters have no implementation and are just documentation
4639
+ if (strictNullChecks && declaration.flags & NodeFlags.Ambient && isParameterDeclaration(declaration)) {
4640
+ parentType = getNonNullableType(parentType);
4641
+ }
4638
4642
4639
4643
let type: Type | undefined;
4640
4644
if (pattern.kind === SyntaxKind.ObjectBindingPattern) {
@@ -4654,53 +4658,13 @@ namespace ts {
4654
4658
else {
4655
4659
// Use explicitly specified property name ({ p: xxx } form), or otherwise the implied name ({ p } form)
4656
4660
const name = declaration.propertyName || <Identifier>declaration.name;
4657
- const isLate = isLateBindableName(name);
4658
- const isWellKnown = isComputedPropertyName(name) && isWellKnownSymbolSyntactically(name.expression);
4659
- if (!isLate && !isWellKnown && isComputedNonLiteralName(name)) {
4660
- const exprType = checkExpression((name as ComputedPropertyName).expression);
4661
- if (isTypeAssignableToKind(exprType, TypeFlags.ESSymbolLike)) {
4662
- if (noImplicitAny) {
4663
- error(declaration, Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(exprType), typeToString(parentType));
4664
- }
4665
- return anyType;
4666
- }
4667
- const indexerType = isTypeAssignableToKind(exprType, TypeFlags.NumberLike) && getIndexTypeOfType(parentType, IndexKind.Number) || getIndexTypeOfType(parentType, IndexKind.String);
4668
- if (!indexerType && noImplicitAny && !compilerOptions.suppressImplicitAnyIndexErrors) {
4669
- if (getIndexTypeOfType(parentType, IndexKind.Number)) {
4670
- error(declaration, Diagnostics.Element_implicitly_has_an_any_type_because_index_expression_is_not_of_type_number);
4671
- }
4672
- else {
4673
- error(declaration, Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature, typeToString(parentType));
4674
- }
4675
- }
4676
- return indexerType || anyType;
4677
- }
4678
-
4679
- // Use type of the specified property, or otherwise, for a numeric name, the type of the numeric index signature,
4680
- // or otherwise the type of the string index signature.
4681
- const nameType = isLate ? checkComputedPropertyName(name as ComputedPropertyName) as LiteralType | UniqueESSymbolType : undefined;
4682
- const text = isLate ? getLateBoundNameFromType(nameType!) :
4683
- isWellKnown ? getPropertyNameForKnownSymbolName(idText(((name as ComputedPropertyName).expression as PropertyAccessExpression).name)) :
4684
- getTextOfPropertyName(name);
4685
-
4686
- // Relax null check on ambient destructuring parameters, since the parameters have no implementation and are just documentation
4687
- if (strictNullChecks && declaration.flags & NodeFlags.Ambient && isParameterDeclaration(declaration)) {
4688
- parentType = getNonNullableType(parentType);
4689
- }
4690
- if (isLate && nameType && !getPropertyOfType(parentType, text) && isTypeAssignableToKind(nameType, TypeFlags.ESSymbolLike)) {
4691
- if (noImplicitAny) {
4692
- error(declaration, Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(nameType), typeToString(parentType));
4693
- }
4694
- return anyType;
4695
- }
4696
- const declaredType = getConstraintForLocation(getTypeOfPropertyOfType(parentType, text), declaration.name);
4697
- type = declaredType && getFlowTypeOfReference(declaration, declaredType) ||
4698
- isNumericLiteralName(text) && getIndexTypeOfType(parentType, IndexKind.Number) ||
4699
- getIndexTypeOfType(parentType, IndexKind.String);
4700
- if (!type) {
4701
- error(name, Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), declarationNameToString(name));
4702
- return errorType;
4703
- }
4661
+ const exprType = isComputedPropertyName(name)
4662
+ ? checkComputedPropertyName(name)
4663
+ : isIdentifier(name)
4664
+ ? getLiteralType(unescapeLeadingUnderscores(name.escapedText))
4665
+ : checkExpression(name);
4666
+ const declaredType = checkIndexedAccessIndexType(getIndexedAccessType(getApparentType(parentType), exprType, name), name);
4667
+ type = getFlowTypeOfReference(declaration, getConstraintForLocation(declaredType, declaration.name));
4704
4668
}
4705
4669
}
4706
4670
else {
@@ -9359,12 +9323,16 @@ namespace ts {
9359
9323
return false;
9360
9324
}
9361
9325
9362
- function getPropertyTypeForIndexType(objectType: Type, indexType: Type, accessNode: ElementAccessExpression | IndexedAccessTypeNode | undefined, cacheSymbol: boolean, missingType: Type) {
9326
+ function getPropertyTypeForIndexType(objectType: Type, indexType: Type, accessNode: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | undefined, cacheSymbol: boolean, missingType: Type) {
9363
9327
const accessExpression = accessNode && accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode : undefined;
9364
- const propName = isTypeUsableAsLateBoundName(indexType) ? getLateBoundNameFromType(indexType) :
9365
- accessExpression && checkThatExpressionIsProperSymbolReference(accessExpression.argumentExpression, indexType, /*reportError*/ false) ?
9366
- getPropertyNameForKnownSymbolName(idText((<PropertyAccessExpression>accessExpression.argumentExpression).name)) :
9367
- undefined;
9328
+ const propName = isTypeUsableAsLateBoundName(indexType)
9329
+ ? getLateBoundNameFromType(indexType)
9330
+ : accessExpression && checkThatExpressionIsProperSymbolReference(accessExpression.argumentExpression, indexType, /*reportError*/ false)
9331
+ ? getPropertyNameForKnownSymbolName(idText((<PropertyAccessExpression>accessExpression.argumentExpression).name))
9332
+ : accessNode && isPropertyName(accessNode)
9333
+ // late bound names are handled in the first branch, so here we only need to handle normal names
9334
+ ? getPropertyNameForPropertyNameNode(accessNode)
9335
+ : undefined;
9368
9336
if (propName !== undefined) {
9369
9337
const prop = getPropertyOfType(objectType, propName);
9370
9338
if (prop) {
@@ -9385,7 +9353,7 @@ namespace ts {
9385
9353
}
9386
9354
if (everyType(objectType, isTupleType) && isNumericLiteralName(propName) && +propName >= 0) {
9387
9355
if (accessNode && everyType(objectType, t => !(<TupleTypeReference>t).target.hasRestElement)) {
9388
- const indexNode = accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode.argumentExpression : accessNode.indexType ;
9356
+ const indexNode = getIndexNodeForAccessExpression( accessNode) ;
9389
9357
error(indexNode, Diagnostics.Property_0_does_not_exist_on_type_1, unescapeLeadingUnderscores(propName), typeToString(objectType));
9390
9358
}
9391
9359
return mapType(objectType, t => getRestTypeOfTupleType(<TupleTypeReference>t) || undefinedType);
@@ -9400,7 +9368,7 @@ namespace ts {
9400
9368
undefined;
9401
9369
if (indexInfo) {
9402
9370
if (accessNode && !isTypeAssignableToKind(indexType, TypeFlags.String | TypeFlags.Number)) {
9403
- const indexNode = accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode.argumentExpression : accessNode.indexType ;
9371
+ const indexNode = getIndexNodeForAccessExpression( accessNode) ;
9404
9372
error(indexNode, Diagnostics.Type_0_cannot_be_used_as_an_index_type, typeToString(indexType));
9405
9373
}
9406
9374
else if (accessExpression && indexInfo.isReadonly && (isAssignmentTarget(accessExpression) || isDeleteTarget(accessExpression))) {
@@ -9441,7 +9409,7 @@ namespace ts {
9441
9409
return anyType;
9442
9410
}
9443
9411
if (accessNode) {
9444
- const indexNode = accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode.argumentExpression : accessNode.indexType ;
9412
+ const indexNode = getIndexNodeForAccessExpression( accessNode) ;
9445
9413
if (indexType.flags & (TypeFlags.StringLiteral | TypeFlags.NumberLiteral)) {
9446
9414
error(indexNode, Diagnostics.Property_0_does_not_exist_on_type_1, "" + (<LiteralType>indexType).value, typeToString(objectType));
9447
9415
}
@@ -9458,6 +9426,16 @@ namespace ts {
9458
9426
return missingType;
9459
9427
}
9460
9428
9429
+ function getIndexNodeForAccessExpression(accessNode: ElementAccessExpression | IndexedAccessTypeNode | PropertyName) {
9430
+ return accessNode.kind === SyntaxKind.ElementAccessExpression
9431
+ ? accessNode.argumentExpression
9432
+ : accessNode.kind === SyntaxKind.IndexedAccessType
9433
+ ? accessNode.indexType
9434
+ : accessNode.kind === SyntaxKind.ComputedPropertyName
9435
+ ? accessNode.expression
9436
+ : accessNode;
9437
+ }
9438
+
9461
9439
function isGenericObjectType(type: Type): boolean {
9462
9440
return maybeTypeOfKind(type, TypeFlags.InstantiableNonPrimitive | TypeFlags.GenericMappedType);
9463
9441
}
@@ -9521,7 +9499,7 @@ namespace ts {
9521
9499
return instantiateType(getTemplateTypeFromMappedType(objectType), templateMapper);
9522
9500
}
9523
9501
9524
- function getIndexedAccessType(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode, missingType = accessNode ? errorType : unknownType): Type {
9502
+ function getIndexedAccessType(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode | PropertyName , missingType = accessNode ? errorType : unknownType): Type {
9525
9503
if (objectType === wildcardType || indexType === wildcardType) {
9526
9504
return wildcardType;
9527
9505
}
@@ -23207,7 +23185,7 @@ namespace ts {
23207
23185
forEach(node.types, checkSourceElement);
23208
23186
}
23209
23187
23210
- function checkIndexedAccessIndexType(type: Type, accessNode: ElementAccessExpression | IndexedAccessTypeNode ) {
23188
+ function checkIndexedAccessIndexType(type: Type, accessNode: Node ) {
23211
23189
if (!(type.flags & TypeFlags.IndexedAccess)) {
23212
23190
return type;
23213
23191
}
0 commit comments