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

Optional properties with object shorthand syntax #39376

Closed
4 of 5 tasks
danielrentz opened this issue Jul 2, 2020 · 4 comments
Closed
4 of 5 tasks

Optional properties with object shorthand syntax #39376

danielrentz opened this issue Jul 2, 2020 · 4 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@danielrentz
Copy link

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 value undefined). 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

interface Test {
  required: number;
  optional?: number
}
let required = 42;
let optional: number | undefined;

Current solution:

const object: Test = { required };
if (optional !== undefined) { object.optional = optional; }

Proposed solution:

const object: Test = { required, optional? };

The compiler would convert this to the solution above with if statements as needed.

Checklist

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.

I have no idea about the last point though... :/ At least I did not find a comparable ES proposal.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jul 3, 2020

Hmm, that's an interesting idea for a design proposal given the sorts of hacky object spread syntax I've seen in the past. It wouldn't really fit in the scope of TS, but it would be a reasonable ES proposal.

@RyanCavanaugh
Copy link
Member

There's always

const object: Test = { required, ...{optional && { optional } } };

I think this is out of scope for TS; it doesn't have anything to do with type checking

@RyanCavanaugh RyanCavanaugh added Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript labels Jul 8, 2020
@danielrentz
Copy link
Author

OK thanks, I'll try my luck with an ES proposal.

@danielrentz
Copy link
Author

For the records. Just found an existing idea in the TC discuss groups: https://es.discourse.group/t/optional-property-shorthand-in-object-literal/196
Not much action since 2018 though...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants