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

Nicer object destructuring in function declarations #52454

Open
uwinkler opened this issue Jan 27, 2023 · 1 comment
Open

Nicer object destructuring in function declarations #52454

uwinkler opened this issue Jan 27, 2023 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@uwinkler
Copy link

Nicer object destructuring in function declarations

πŸ” Search Terms

object destructuring function declarations

βœ… Viability Checklist

My suggestion meets these guidelines (I guess):

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

⭐ Suggestion

Object destruction in functions can be cumbersome. Using defaults makes it slightly easier, but it would be great if it could be written in a simpler way.

πŸ“ƒ Motivating Example

// The current way of doing object destructuring in function declarations
// is cumbersome, lengthy, and repetitive  
// πŸ₯Ί
function Rectangle({
  width,
  height,
  fill
}: {
  width: number
  height: number
  fill: 'dark' | 'light'
}) {}
 

// 😍 ... maybe this could be done "nicer" by just providing the type?
function Rectangle({width:number, height:number, fill: 'dark' | 'light'}) {}

// πŸ€” Currently, we can use defaults to achieve something along this line:
function Rectangle({width = 200, height = 200, fill = 'dark'}) { }
// ... , but `fill` would not be typed correctly πŸ₯Ί and maybe we don't want 
// to use default values in the first place

// 😍 Could this be the way to do it with correctly typed defaults? 
function Rectangle({width = 200, height = 200, fill: 'dark' | 'light' = 'dark'}) {}

// 😍 ...or kind of "mixed" mode? 
function Rectangle({width = 200, height = 200, fill: 'dark' | 'light'}) {}

πŸ’» Use Cases

This is a common pattern in React code.

@MartinJohns
Copy link
Contributor

Duplicate of #29526.

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label Jan 29, 2023
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