-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Improved checking of destructuring with literal initializers #4598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
e78478b
c436736
f28e424
66e3aba
dc8ad6e
e40b86f
bb81797
a0ddd43
f801420
31f8a81
546da60
27380f4
055363c
d5ff9ee
a95c423
273b9ff
262f122
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2318,8 +2318,8 @@ namespace ts { | |
// Use type of the specified property, or otherwise, for a numeric name, the type of the numeric index signature, | ||
// or otherwise the type of the string index signature. | ||
type = getTypeOfPropertyOfType(parentType, name.text) || | ||
isNumericLiteralName(name.text) && getIndexTypeOfType(parentType, IndexKind.Number) || | ||
getIndexTypeOfType(parentType, IndexKind.String); | ||
isNumericLiteralName(name.text) && getIndexTypeOfType(parentType, IndexKind.Number) || | ||
getIndexTypeOfType(parentType, IndexKind.String); | ||
if (!type) { | ||
error(name, Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), declarationNameToString(name)); | ||
return unknownType; | ||
|
@@ -2456,7 +2456,6 @@ namespace ts { | |
let unionOfElements = getUnionType(elementTypes); | ||
return languageVersion >= ScriptTarget.ES6 ? createIterableType(unionOfElements) : createArrayType(unionOfElements); | ||
} | ||
|
||
// If the pattern has at least one element, and no rest element, then it should imply a tuple type. | ||
return createTupleType(elementTypes); | ||
} | ||
|
@@ -6607,12 +6606,18 @@ namespace ts { | |
} | ||
} | ||
if (isBindingPattern(declaration.name)) { | ||
return getTypeFromBindingPattern(<BindingPattern>declaration.name); | ||
return createImpliedType(getTypeFromBindingPattern(<BindingPattern>declaration.name)); | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
function createImpliedType(type: Type): Type { | ||
var result = clone(type); | ||
result.flags |= TypeFlags.ImpliedType; | ||
return result; | ||
} | ||
|
||
function getContextualTypeForReturnExpression(node: Expression): Type { | ||
let func = getContainingFunction(node); | ||
if (func && !func.asteriskToken) { | ||
|
@@ -7005,9 +7010,6 @@ namespace ts { | |
|
||
function checkArrayLiteral(node: ArrayLiteralExpression, contextualMapper?: TypeMapper): Type { | ||
let elements = node.elements; | ||
if (!elements.length) { | ||
return createArrayType(undefinedType); | ||
} | ||
let hasSpreadElement = false; | ||
let elementTypes: Type[] = []; | ||
let inDestructuringPattern = isAssignmentTarget(node); | ||
|
@@ -7039,12 +7041,28 @@ namespace ts { | |
hasSpreadElement = hasSpreadElement || e.kind === SyntaxKind.SpreadElementExpression; | ||
} | ||
if (!hasSpreadElement) { | ||
// If array literal is actually a destructuring pattern, mark it as an implied type. We do this such | ||
// that we get the same behavior for "var [x, y] = []" and "[x, y] = []". | ||
if (inDestructuringPattern && elementTypes.length) { | ||
return createImpliedType(createTupleType(elementTypes)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't you have to do this for object literals too (the ones that happen to be assignment patterns)? Also, I don't think there are any tests for assignment patterns, just binding patterns. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, actually maybe you did not mean to do that. Here you are creating an implied type for an assignment pattern. But I think only binding patterns can imply types. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want to allow There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kind of think that for parity with arrays, it's nice to do it for object literals too. Though I realize that the main rationale for arrays is to make the right hand side a tuple. |
||
} | ||
let contextualType = getContextualType(node); | ||
if (contextualType && contextualTypeIsTupleLikeType(contextualType) || inDestructuringPattern) { | ||
return createTupleType(elementTypes); | ||
let contextualTupleLikeType = contextualType && contextualTypeIsTupleLikeType(contextualType) ? contextualType : undefined; | ||
if (contextualTupleLikeType) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's simpler to just do if (contextualType && contextualTypeIsTupleLikeType(contextualType)) {
...
} And just refer to contextualType inside. Essentially just inline the condition There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. |
||
// If array literal is contextually typed by the implied type of a binding pattern, pad the resulting | ||
// tuple type with elements from the binding tuple type to make the lengths equal. | ||
if (contextualTupleLikeType.flags & TypeFlags.Tuple && contextualTupleLikeType.flags & TypeFlags.ImpliedType) { | ||
let contextualElementTypes = (<TupleType>contextualTupleLikeType).elementTypes; | ||
for (let i = elementTypes.length; i < contextualElementTypes.length; i++) { | ||
elementTypes.push(contextualElementTypes[i]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, maybe I'm missing something. But it seems like only initialized properties should be copied, otherwise you allow default initializers that are too short to satisfy the destructuring. |
||
} | ||
} | ||
if (elementTypes.length) { | ||
return createTupleType(elementTypes); | ||
} | ||
} | ||
} | ||
return createArrayType(getUnionType(elementTypes)); | ||
return createArrayType(elementTypes.length ? getUnionType(elementTypes) : undefinedType) | ||
} | ||
|
||
function isNumericName(name: DeclarationName): boolean { | ||
|
@@ -7131,6 +7149,14 @@ namespace ts { | |
} | ||
typeFlags |= type.flags; | ||
let prop = <TransientSymbol>createSymbol(SymbolFlags.Property | SymbolFlags.Transient | member.flags, member.name); | ||
// If object literal is contextually typed by the implied type of a binding pattern, and if the | ||
// binding pattern specifies a default value for the property, make the property optional. | ||
if (contextualType && contextualType.flags & TypeFlags.ImpliedType) { | ||
let impliedProp = getPropertyOfType(contextualType, member.name); | ||
if (impliedProp) { | ||
prop.flags |= impliedProp.flags & SymbolFlags.Optional; | ||
} | ||
} | ||
prop.declarations = member.declarations; | ||
prop.parent = member.parent; | ||
if (member.valueDeclaration) { | ||
|
@@ -7157,6 +7183,17 @@ namespace ts { | |
propertiesArray.push(member); | ||
} | ||
|
||
// If object literal is contextually typed by the implied type of a binding pattern, augment the result | ||
// type with those properties for which the binding pattern specifies a default value. | ||
if (contextualType && contextualType.flags & TypeFlags.ImpliedType) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to add tests that demonstrate that this can cause an error. Something like this, which I don't believe would have errored before: function foo({ a = 0 } = {}) { }
foo({ a: "" }); Basically something where the property was dropped in the initializer, but then was present again in the argument with the wrong type. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It did error before (complaining that the object literal is missing property There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But it does error now because of the string, correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it does error now. |
||
for (let prop of getPropertiesOfType(contextualType)) { | ||
if (prop.flags & SymbolFlags.Optional && !hasProperty(propertiesTable, prop.name)) { | ||
propertiesTable[prop.name] = prop; | ||
propertiesArray.push(prop); | ||
} | ||
} | ||
} | ||
|
||
let stringIndexType = getIndexType(IndexKind.String); | ||
let numberIndexType = getIndexType(IndexKind.Number); | ||
let result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexType, numberIndexType); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this step applies both for top level initializers as well as initializers nested in binding patterns?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it applies generally. The level of nesting shouldn't matter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have any tests that exercise it in the nested case?