Skip to content

new short for expression variable == null ? value : variable; ts(2345) #61779

Closed as not planned
@mathematicalsystems

Description

@mathematicalsystems

πŸ” Search Terms

short expression variable == null ? value : variable; ts(2345)
Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
Type 'undefined' is not assignable to type 'number

βœ… Viability Checklist

⭐ Suggestion

the expression variable == null ? value : variable; is mandatory in many places to add but it takes space of to write, so i suggest reduce this famously repeating example with variabl?value which shall equal to variable == null ? value : variable

πŸ“ƒ Motivating Example

for example calling a method with some simple undefined variable this would lead to long line of code due to the warning:
Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
Type 'undefined' is not assignable to type 'number
so if you have variables like
some_variable_a: number | undefined;
some_variable_b: number | undefined;
some_variable_c: number | undefined;

and a method like:
some_method(n_a: number, n_b: number, n_c: number) {}

in regular way would be called this way

this.some_method(this.some_variable_a == null ? alternative_a : this.some_variable_a, this.some_variable_b == null ? alternative_b : this.some_variable_b, this.some_variable_c == null ? alternative_c : this.some_variable_c);

this.some_method(this.some_variable_a?alternative_a, this.some_variable_b?alternative_b, this.some_variable_c?alternative_c);

πŸ’» Use Cases

  1. What do you want to use this for? to easily suppress ts(2345)
  2. What shortcomings exist with current approaches? long text of code
  3. What workarounds are you using in the meantime?none

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