Skip to content

Commit b0a5337

Browse files
committed
Merge branch 'master' into genericRest
# Conflicts: # tests/baselines/reference/objectRest.errors.txt # tests/baselines/reference/objectRest.types
2 parents 18f80b8 + deeee77 commit b0a5337

File tree

52 files changed

+762
-350
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+762
-350
lines changed

src/compiler/checker.ts

Lines changed: 35 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4628,6 +4628,10 @@ namespace ts {
46284628
if (isTypeAny(parentType)) {
46294629
return parentType;
46304630
}
4631+
// Relax null check on ambient destructuring parameters, since the parameters have no implementation and are just documentation
4632+
if (strictNullChecks && declaration.flags & NodeFlags.Ambient && isParameterDeclaration(declaration)) {
4633+
parentType = getNonNullableType(parentType);
4634+
}
46314635

46324636
let type: Type | undefined;
46334637
if (pattern.kind === SyntaxKind.ObjectBindingPattern) {
@@ -4647,53 +4651,13 @@ namespace ts {
46474651
else {
46484652
// Use explicitly specified property name ({ p: xxx } form), or otherwise the implied name ({ p } form)
46494653
const name = declaration.propertyName || <Identifier>declaration.name;
4650-
const isLate = isLateBindableName(name);
4651-
const isWellKnown = isComputedPropertyName(name) && isWellKnownSymbolSyntactically(name.expression);
4652-
if (!isLate && !isWellKnown && isComputedNonLiteralName(name)) {
4653-
const exprType = checkExpression((name as ComputedPropertyName).expression);
4654-
if (isTypeAssignableToKind(exprType, TypeFlags.ESSymbolLike)) {
4655-
if (noImplicitAny) {
4656-
error(declaration, Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(exprType), typeToString(parentType));
4657-
}
4658-
return anyType;
4659-
}
4660-
const indexerType = isTypeAssignableToKind(exprType, TypeFlags.NumberLike) && getIndexTypeOfType(parentType, IndexKind.Number) || getIndexTypeOfType(parentType, IndexKind.String);
4661-
if (!indexerType && noImplicitAny && !compilerOptions.suppressImplicitAnyIndexErrors) {
4662-
if (getIndexTypeOfType(parentType, IndexKind.Number)) {
4663-
error(declaration, Diagnostics.Element_implicitly_has_an_any_type_because_index_expression_is_not_of_type_number);
4664-
}
4665-
else {
4666-
error(declaration, Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature, typeToString(parentType));
4667-
}
4668-
}
4669-
return indexerType || anyType;
4670-
}
4671-
4672-
// Use type of the specified property, or otherwise, for a numeric name, the type of the numeric index signature,
4673-
// or otherwise the type of the string index signature.
4674-
const nameType = isLate ? checkComputedPropertyName(name as ComputedPropertyName) as LiteralType | UniqueESSymbolType : undefined;
4675-
const text = isLate ? getLateBoundNameFromType(nameType!) :
4676-
isWellKnown ? getPropertyNameForKnownSymbolName(idText(((name as ComputedPropertyName).expression as PropertyAccessExpression).name)) :
4677-
getTextOfPropertyName(name);
4678-
4679-
// Relax null check on ambient destructuring parameters, since the parameters have no implementation and are just documentation
4680-
if (strictNullChecks && declaration.flags & NodeFlags.Ambient && isParameterDeclaration(declaration)) {
4681-
parentType = getNonNullableType(parentType);
4682-
}
4683-
if (isLate && nameType && !getPropertyOfType(parentType, text) && isTypeAssignableToKind(nameType, TypeFlags.ESSymbolLike)) {
4684-
if (noImplicitAny) {
4685-
error(declaration, Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(nameType), typeToString(parentType));
4686-
}
4687-
return anyType;
4688-
}
4689-
const declaredType = getConstraintForLocation(getTypeOfPropertyOfType(parentType, text), declaration.name);
4690-
type = declaredType && getFlowTypeOfReference(declaration, declaredType) ||
4691-
isNumericLiteralName(text) && getIndexTypeOfType(parentType, IndexKind.Number) ||
4692-
getIndexTypeOfType(parentType, IndexKind.String);
4693-
if (!type) {
4694-
error(name, Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), declarationNameToString(name));
4695-
return errorType;
4696-
}
4654+
const exprType = isComputedPropertyName(name)
4655+
? checkComputedPropertyName(name)
4656+
: isIdentifier(name)
4657+
? getLiteralType(unescapeLeadingUnderscores(name.escapedText))
4658+
: checkExpression(name);
4659+
const declaredType = checkIndexedAccessIndexType(getIndexedAccessType(getApparentType(parentType), exprType, name), name);
4660+
type = getFlowTypeOfReference(declaration, getConstraintForLocation(declaredType, declaration.name));
46974661
}
46984662
}
46994663
else {
@@ -9360,12 +9324,16 @@ namespace ts {
93609324
return false;
93619325
}
93629326

9363-
function getPropertyTypeForIndexType(objectType: Type, indexType: Type, accessNode: ElementAccessExpression | IndexedAccessTypeNode | undefined, cacheSymbol: boolean, missingType: Type) {
9327+
function getPropertyTypeForIndexType(objectType: Type, indexType: Type, accessNode: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | undefined, cacheSymbol: boolean, missingType: Type) {
93649328
const accessExpression = accessNode && accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode : undefined;
9365-
const propName = isTypeUsableAsLateBoundName(indexType) ? getLateBoundNameFromType(indexType) :
9366-
accessExpression && checkThatExpressionIsProperSymbolReference(accessExpression.argumentExpression, indexType, /*reportError*/ false) ?
9367-
getPropertyNameForKnownSymbolName(idText((<PropertyAccessExpression>accessExpression.argumentExpression).name)) :
9368-
undefined;
9329+
const propName = isTypeUsableAsLateBoundName(indexType)
9330+
? getLateBoundNameFromType(indexType)
9331+
: accessExpression && checkThatExpressionIsProperSymbolReference(accessExpression.argumentExpression, indexType, /*reportError*/ false)
9332+
? getPropertyNameForKnownSymbolName(idText((<PropertyAccessExpression>accessExpression.argumentExpression).name))
9333+
: accessNode && isPropertyName(accessNode)
9334+
// late bound names are handled in the first branch, so here we only need to handle normal names
9335+
? getPropertyNameForPropertyNameNode(accessNode)
9336+
: undefined;
93699337
if (propName !== undefined) {
93709338
const prop = getPropertyOfType(objectType, propName);
93719339
if (prop) {
@@ -9386,7 +9354,7 @@ namespace ts {
93869354
}
93879355
if (everyType(objectType, isTupleType) && isNumericLiteralName(propName) && +propName >= 0) {
93889356
if (accessNode && everyType(objectType, t => !(<TupleTypeReference>t).target.hasRestElement)) {
9389-
const indexNode = accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode.argumentExpression : accessNode.indexType;
9357+
const indexNode = getIndexNodeForAccessExpression(accessNode);
93909358
error(indexNode, Diagnostics.Property_0_does_not_exist_on_type_1, unescapeLeadingUnderscores(propName), typeToString(objectType));
93919359
}
93929360
return mapType(objectType, t => getRestTypeOfTupleType(<TupleTypeReference>t) || undefinedType);
@@ -9401,7 +9369,7 @@ namespace ts {
94019369
undefined;
94029370
if (indexInfo) {
94039371
if (accessNode && !isTypeAssignableToKind(indexType, TypeFlags.String | TypeFlags.Number)) {
9404-
const indexNode = accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode.argumentExpression : accessNode.indexType;
9372+
const indexNode = getIndexNodeForAccessExpression(accessNode);
94059373
error(indexNode, Diagnostics.Type_0_cannot_be_used_as_an_index_type, typeToString(indexType));
94069374
}
94079375
else if (accessExpression && indexInfo.isReadonly && (isAssignmentTarget(accessExpression) || isDeleteTarget(accessExpression))) {
@@ -9442,7 +9410,7 @@ namespace ts {
94429410
return anyType;
94439411
}
94449412
if (accessNode) {
9445-
const indexNode = accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode.argumentExpression : accessNode.indexType;
9413+
const indexNode = getIndexNodeForAccessExpression(accessNode);
94469414
if (indexType.flags & (TypeFlags.StringLiteral | TypeFlags.NumberLiteral)) {
94479415
error(indexNode, Diagnostics.Property_0_does_not_exist_on_type_1, "" + (<LiteralType>indexType).value, typeToString(objectType));
94489416
}
@@ -9459,6 +9427,16 @@ namespace ts {
94599427
return missingType;
94609428
}
94619429

9430+
function getIndexNodeForAccessExpression(accessNode: ElementAccessExpression | IndexedAccessTypeNode | PropertyName) {
9431+
return accessNode.kind === SyntaxKind.ElementAccessExpression
9432+
? accessNode.argumentExpression
9433+
: accessNode.kind === SyntaxKind.IndexedAccessType
9434+
? accessNode.indexType
9435+
: accessNode.kind === SyntaxKind.ComputedPropertyName
9436+
? accessNode.expression
9437+
: accessNode;
9438+
}
9439+
94629440
function isGenericObjectType(type: Type): boolean {
94639441
return maybeTypeOfKind(type, TypeFlags.InstantiableNonPrimitive | TypeFlags.GenericMappedType);
94649442
}
@@ -9522,7 +9500,7 @@ namespace ts {
95229500
return instantiateType(getTemplateTypeFromMappedType(objectType), templateMapper);
95239501
}
95249502

9525-
function getIndexedAccessType(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode, missingType = accessNode ? errorType : unknownType): Type {
9503+
function getIndexedAccessType(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode | PropertyName, missingType = accessNode ? errorType : unknownType): Type {
95269504
if (objectType === wildcardType || indexType === wildcardType) {
95279505
return wildcardType;
95289506
}
@@ -23208,7 +23186,7 @@ namespace ts {
2320823186
forEach(node.types, checkSourceElement);
2320923187
}
2321023188

23211-
function checkIndexedAccessIndexType(type: Type, accessNode: ElementAccessExpression | IndexedAccessTypeNode) {
23189+
function checkIndexedAccessIndexType(type: Type, accessNode: Node) {
2321223190
if (!(type.flags & TypeFlags.IndexedAccess)) {
2321323191
return type;
2321423192
}

0 commit comments

Comments
 (0)