Optional properties with object shorthand syntax #39376
Labels
Out of Scope
This idea sits outside of the TypeScript language design constraints
Suggestion
An idea for TypeScript
Search Terms
object shorthand optional
Suggestion
Allow to specify optional properties in object shorthand syntax. If the variable is
undefined
, the property will not be added to the object (instead of being added with valueundefined
). Proposed syntax is to add a question mark to the property name, for example{ prop1, prop2? }
.Use Cases
Goal is to create one object with a short clear (one-line) syntax.
On StackOverflow, this issue is discussed here: https://stackoverflow.com/questions/54909137/how-to-conditionally-set-optional-properties
The proposed solution uses object spread syntax. But this may have performance impact (temporary objects, property iteration) and is way too verbose.
Examples
Current solution:
Proposed solution:
The compiler would convert this to the solution above with if statements as needed.
Checklist
My suggestion meets these guidelines:
I have no idea about the last point though... :/ At least I did not find a comparable ES proposal.
The text was updated successfully, but these errors were encountered: