Skip to content

Binding element that is a rest element should always be an array #2085

Closed

Description

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".

Activity

  1. added
    BugA bug in TypeScript
    SpecIssues related to the TypeScript language specification
    on Feb 20, 2015
  2. danquirk commented on Feb 20, 2015

    @danquirk
    Member

    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.

  3. added
    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead
    and removed
    BugA bug in TypeScript
    on Feb 20, 2015
  4. osdm commented on Feb 20, 2015

    @osdm
    Author

    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?

  5. osdm commented on Feb 20, 2015

    @osdm
    Author

    For example, what type should have "b" in this example?

    function f([a, ...b]) { }
    
  6. danquirk commented on Feb 20, 2015

    @danquirk
    Member

    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).

  7. osdm commented on Feb 20, 2015

    @osdm
    Author

    Ok, another example.

    var c: any;
    var [a, ...b] = c;
    

    6.4 doesn't come into play. b is any instead of any[].

  8. locked and limited conversation to collaborators on Jun 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" insteadSpecIssues related to the TypeScript language specification

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions