Skip to content
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

Inline type annotations for rebinded destructuring parameters #3828

Closed
d180cf opened this issue Jul 11, 2015 · 5 comments
Closed

Inline type annotations for rebinded destructuring parameters #3828

d180cf opened this issue Jul 11, 2015 · 5 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints

Comments

@d180cf
Copy link

d180cf commented Jul 11, 2015

I've seen a discussion around this in Flow, but couldn't find one here. The discussion was around this (desired, but not actual) syntax:

function fn({ a: number, b: string as b2 }) {
  return a + b2;
}

This is what a ts user expects to write and this is what I often mistakenly write despite I'm quite familiar with ts. The problem with this syntax is known: it conflicts with the ES6 spec which says that { a: number } means let number = arguments[0].a

However, in the syntax for importing stuff from modules, ES6 spec uses a different syntax:

import { each as forEach } from "jQuery";

Which I personally find easier to understand, but what's more important is that it leaves a room for a ts type annotation:

import { each: Function as forEach } from "jQuery";

What I'm asking for is probably an unheard of thing: can ts deviate from the ES6 spec in this particular place and treat { b: string as b2 } syntax as "parameter b of type string rebinded to variable b2"?

@DanielRosenwasser
Copy link
Member

I don't think we could use that specific ordering given that we've added a new as type assertion operator (written in the form value as Type).

@DanielRosenwasser DanielRosenwasser added the Suggestion An idea for TypeScript label Jul 12, 2015
@d180cf
Copy link
Author

d180cf commented Jul 12, 2015

The as operator seems like another way of casting the type and it can't appear in { b: string as b2 } syntax.

@DanielRosenwasser
Copy link
Member

Sorry, what I really meant by that is that it would be confusing to do so. Users would have to remember that

  1. imports use as and object binding patterns use : (already slightly annoying to remember)
  2. the as operator is ordered as expression as Type (not much of a problem),
  3. to give an inline type in an object binding pattern, the syntax is : Type as name (not consistent with the other two).

@d180cf
Copy link
Author

d180cf commented Jul 12, 2015

In #1 I'm suggesting to use "as" in object binding patterns as well (which contradicts to ES6 and may have problems with nested destructuring): const {b as b2} = fn();

@RyanCavanaugh RyanCavanaugh added the Out of Scope This idea sits outside of the TypeScript language design constraints label Jul 13, 2015
@RyanCavanaugh
Copy link
Member

We need to just leave this area alone -- there's too much room for conflict with ES7+ activity, and adding any more syntax or variations is more likely to increase confusion than decrease it (as we've seen with the ES6 module import syntax).

@danquirk danquirk removed the Suggestion An idea for TypeScript label Jul 13, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints
Projects
None yet
Development

No branches or pull requests

4 participants