Description
π 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
- 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, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β 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
- What do you want to use this for? to easily suppress ts(2345)
- What shortcomings exist with current approaches? long text of code
- What workarounds are you using in the meantime?none