You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Binding element that is a rest element should always be an array #2085
In specification 5.1.2, after line "If the binding element specifies an initializer expression, T is the type of that initializer expression" I suggest adding a line:
"Otherwise, if the binding element is a rest element, T is an array type with an element type any".
I believe the current spec is correct. This is the prose associated with binding element:
The type T associated with a binding element is determined as follows:
• Let S be the type associated with the immediately containing destructuring variable declaration, binding property, or binding element.
• If S is the Any type:
o If the binding element specifies an initializer expression, T is the type of that initializer expression.
o Otherwise, T is the Any type.
• If S is not an array-like type (section 3.3.2), no type is associated with the binding property and an error occurs.
• If the binding element is a rest element, T is an array type with an element type E, where E is the type of the numeric index signature of S.
• Otherwise, if S is a tuple-like type (section 3.3.3):
o Let N be the zero-based index of the binding element in the array binding pattern.
o If S has a property with the numerical name N, T is the type of that property.
o Otherwise, no type is associated with the binding element and an error occurs.
• Otherwise, if S has a numeric index signature, T is the type of the numeric index signature.
• Otherwise, no type is associated with the binding element and an error occurs.
You need to also look at 5.1.3 Implied Type: If the declaration specifies an array binding pattern with a rest element, the implied type is an array type with an element type of Any.
which then comes into play with this clause in 5.1.2: If the binding element is a rest element, T is an array type with an element type E, where E is the type of the numeric index signature of S.
where S in 5.1.2 is the T defined by this clause in 6.4: Otherwise, if the declaration specifies a binding pattern, T is the implied type of that binding pattern (section 5.1.3).
Activity
danquirk commentedon Feb 20, 2015
I believe the current spec is correct. This is the prose associated with binding element:
The type T associated with a binding element is determined as follows:
• Let S be the type associated with the immediately containing destructuring variable declaration, binding property, or binding element.
• If S is the Any type:
o If the binding element specifies an initializer expression, T is the type of that initializer expression.
o Otherwise, T is the Any type.
• If S is not an array-like type (section 3.3.2), no type is associated with the binding property and an error occurs.
• If the binding element is a rest element, T is an array type with an element type E, where E is the type of the numeric index signature of S.
• Otherwise, if S is a tuple-like type (section 3.3.3):
o Let N be the zero-based index of the binding element in the array binding pattern.
o If S has a property with the numerical name N, T is the type of that property.
o Otherwise, no type is associated with the binding element and an error occurs.
• Otherwise, if S has a numeric index signature, T is the type of the numeric index signature.
• Otherwise, no type is associated with the binding element and an error occurs.
osdm commentedon Feb 20, 2015
If S is the Any type and there is no initializer expression, then T would be Any type. How is this correct for a rest element?
osdm commentedon Feb 20, 2015
For example, what type should have "b" in this example?
danquirk commentedon Feb 20, 2015
You need to also look at 5.1.3 Implied Type:
If the declaration specifies an array binding pattern with a rest element, the implied type is an array type with an element type of Any.
which then comes into play with this clause in 5.1.2:
If the binding element is a rest element, T is an array type with an element type E, where E is the type of the numeric index signature of S.
where S in 5.1.2 is the T defined by this clause in 6.4:
Otherwise, if the declaration specifies a binding pattern, T is the implied type of that binding pattern (section 5.1.3).
osdm commentedon Feb 20, 2015
Ok, another example.
6.4 doesn't come into play. b is any instead of any[].