Skip to content

Optional properties with object shorthand syntax #39376

Description

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.

Activity

  1. DanielRosenwasser commented on Jul 3, 2020

    @DanielRosenwasser
    Member

    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.

  2. RyanCavanaugh commented on Jul 8, 2020

    @RyanCavanaugh
    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

  3. danielrentz commented on Jul 9, 2020

    @danielrentz
    Author

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

  4. danielrentz commented on Jul 23, 2020

    @danielrentz
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions