You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like a type that describes an optional parameter - similar to the existing ?, but set as a type rather than as a parameter modifier. This would allow parameters to be conditionally optional.
JavaScript already allows for optional parameters (defaulting the value to undefined) so this wouldn't be a change to existing JavaScript code. The compiled JavaScript code wouldn't change in any way. Since the effect can be simulated (see Use Cases), this feature request is mainly TypeScript syntactic sugar.
π Motivating Example
When defining a class, it can be useful to make a parameter conditionally optional.
For example, consider the following class. it has a single method that takes 2 parameters: a string (the key defined in a map) and the type of data that map has defined.
classFoo<TMap={}>{doSomething<KextendskeyofTMap>(key: K,datum: TMap[K]){console.log({ key, datum });}}
At the moment, the doSomething method requires 2 parameters even if TMap[K] evaluates to undefined.
constfoo=newFoo<{one: string,two: undefined}>();foo.doSomething("one","a");// validfoo.doSomething("one");// ts(2554) as expectedfoo.doSomething("two",undefined);// validfoo.doSomething("two");// ts(2554) sadly
A way of telling TypeScript that the parameter may be optional in some circumstances would be useful.
classFoo<TMap={}>{doSomething<KextendskeyofTMap>(key: K,datum: TMap[K]extendsundefined ? omitted|undefined : TMap[K]){console.log({ key, datum });}}constfoo=newFoo<{one: string,two: undefined}>();foo.doSomething("one","a");// validfoo.doSomething("one");// ts(2554) as expectedfoo.doSomething("two",undefined);// validfoo.doSomething("two");// valid
π» Use Cases
What do you want to use this for?
I want to use this for an observer that I'm building. Sometimes I need to pass data to the event, but other times I don't. It would save me some typing if I didn't have to pass undefined manually, or add in workarounds to make the parameter conditionally optional.
What shortcomings exist with current approaches?
At the moment, the way to define an optional parameter is using the ? parameter modifier, but this makes the parameter optional even when it shouldn't be.
classFoo<TMap={}>{doSomething<KextendskeyofTMap>(key: K,datum?: TMap[K]){console.log({ key, datum });}}constfoo=newFoo<{one: string,two: undefined}>();foo.doSomething("one","a");foo.doSomething("one");// no ts(2554)
What workarounds are you using in the meantime?
It's possible to simulate this behaviour using a rest parameter, but it requires the parameter to be defined as both an empty array and an array containing undefined. The variable itself also needs to be extracted before it can be used.
π Search Terms
"optional", "parameter"
β Viability Checklist
β Suggestion
I would like a type that describes an optional parameter - similar to the existing
?
, but set as a type rather than as a parameter modifier. This would allow parameters to be conditionally optional.JavaScript already allows for optional parameters (defaulting the value to
undefined
) so this wouldn't be a change to existing JavaScript code. The compiled JavaScript code wouldn't change in any way. Since the effect can be simulated (see Use Cases), this feature request is mainly TypeScript syntactic sugar.π Motivating Example
When defining a class, it can be useful to make a parameter conditionally optional.
For example, consider the following class. it has a single method that takes 2 parameters: a string (the key defined in a map) and the type of data that map has defined.
At the moment, the
doSomething
method requires 2 parameters even ifTMap[K]
evaluates toundefined
.A way of telling TypeScript that the parameter may be optional in some circumstances would be useful.
π» Use Cases
I want to use this for an observer that I'm building. Sometimes I need to pass data to the event, but other times I don't. It would save me some typing if I didn't have to pass
undefined
manually, or add in workarounds to make the parameter conditionally optional.At the moment, the way to define an optional parameter is using the
?
parameter modifier, but this makes the parameter optional even when it shouldn't be.It's possible to simulate this behaviour using a rest parameter, but it requires the parameter to be defined as both an empty array and an array containing
undefined
. The variable itself also needs to be extracted before it can be used.The text was updated successfully, but these errors were encountered: