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

Destructure parameters and type at the same time. #34748

Closed
4 of 5 tasks
danm opened this issue Oct 26, 2019 · 3 comments
Closed
4 of 5 tasks

Destructure parameters and type at the same time. #34748

danm opened this issue Oct 26, 2019 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@danm
Copy link

danm commented Oct 26, 2019

name: Destructure function parameters review
about: Allow types to be added to destructured function parameters for better readability

Despite what https://github.com/Microsoft/TypeScript/wiki/FAQ#functions says, can we not type cast parameters using instead?

I destructure my parameters a lot, and having to create another curly brace afterwards makes the code messy. I abstract the parameter type to an interface most of the time, but in some cases, it would just be nicer to add types right there whilst destructuring.

so how about...

function f({x : <number>}) {
  console.log(x);
}

with alias'

function f({ x: aliasX<number>}) {
  console.log(x);
}

with optional

function ({ x?: <number>}) {
  console.log(x);
}

with defaults

function f({ x = 10<number>}) {
  console.log(x);
}

with defaults and alias

function ({ x: aliasX<number> = 10 }) {
  console.log(x);
}

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@MartinJohns
Copy link
Contributor

You might want to check out the relevant issue template.

@danm
Copy link
Author

danm commented Oct 27, 2019

thanks @MartinJohns,
I have updated the question.

It still stands though, I haven't seen other comments about why brackets couldn't be used.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 30, 2019
@RyanCavanaugh
Copy link
Member

See #29526 for discussion of this topic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants