Skip to content

Contextual typing of destructuring parameters is not described in the spec #2006

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

Closed
osdm opened this issue Feb 11, 2015 · 3 comments
Closed
Assignees
Labels
Fixed A PR has been merged for this issue Spec Issues related to the TypeScript language specification

Comments

@osdm
Copy link

osdm commented Feb 11, 2015

Contextual typing of destructuring parameters seems to be an obvious idea, but I could not find any mention of this in 1.5 spec draft in master branch. Just wanted to know if you have plans to do it or not, so we could support it in our external tool.

interface A { field1: number; field2: number }
function f(p: (p1: A) => number)  {}
f(({field1, field2}) => {})
@DanielRosenwasser
Copy link
Member

In section 4.19 of the current spec in master:

  • In a variable, parameter, binding property, binding element, or member declaration, an initializer expression is contextually typed by
    • the type given in the declaration's type annotation, if any, or otherwise
    • for a parameter, the contextual type for the declaration (section 4.9.3), if any, or otherwise
    • the type implied by the binding pattern in the declaration (section 5.1.3), if any.

I think what you're looking for is information on binding patterns and implied types.

The view of things for our compiler is that destructuring is the name of a concept that encompasses the use of binding patterns to extract information from an object in a convenient way.

@osdm
Copy link
Author

osdm commented Feb 11, 2015

Sorry, I'm not really talking about contextually typing "initializer expression", see my code sample. I'm talking about type associated with a parameter, see section 6.4:

The type of local introduced in a destructuring parameter declaration is determined in the same manner
as a local introduced by a destructuring variable declaration, except the type T associated with a
destructuring parameter declaration is determined as follows:

  • If the declaration includes a type annotation, T is that type.
  • Otherwise, if the declaration includes an initializer expression, T is the widened form (section 3.11)
    of the type of the initializer expression.
  • Otherwise, if the declaration specifies a binding pattern, T is the implied type of that binding
    pattern (section 5.1.3).
  • Otherwise, if the parameter is a rest parameter, T is any[].
  • Otherwise, T is any.

Not a word about contextual typing here, but section 4.9.3 says:

When a function expression with no type parameters and no parameter type annotations is contextually
typed (section 4.19) by a type T and a contextual signature S can be extracted from T, the function
expression is processed as if it had explicitly specified parameter type annotations as they exist in S.

Does that mean that contextual types affect type associated with a parameter, or not? I suggest explicitly mentioning this in the spec or (better) providing an example.

@danquirk danquirk added the Spec Issues related to the TypeScript language specification label Feb 11, 2015
@mhegazy mhegazy added the Bug A bug in TypeScript label Mar 24, 2015
@mhegazy mhegazy added this to the TypeScript 1.6 milestone Mar 24, 2015
@mhegazy mhegazy removed the Bug A bug in TypeScript label May 27, 2015
@ahejlsberg
Copy link
Member

Fixed in #4033.

@DanielRosenwasser DanielRosenwasser added the Fixed A PR has been merged for this issue label Aug 6, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue Spec Issues related to the TypeScript language specification
Projects
None yet
Development

No branches or pull requests

5 participants