TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Optional parameter type

Open Skateside opened this issue 1 year ago • 1 comments

🔍 Search Terms

"optional", "parameter"

✅ Viability Checklist

  • [X] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [X] This wouldn't change the runtime behavior of existing JavaScript code
  • [X] This could be implemented without emitting different JS based on the types of the expressions
  • [X] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • [X] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
  • [X] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

⭐ 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.

class Foo<TMap = {}> {
    doSomething<K extends keyof TMap>(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.

const foo = new Foo<{ one: string, two: undefined }>();
foo.doSomething("one", "a");        // valid
foo.doSomething("one");             // ts(2554) as expected
foo.doSomething("two", undefined);  // valid
foo.doSomething("two");             // ts(2554) sadly

A way of telling TypeScript that the parameter may be optional in some circumstances would be useful.

class Foo<TMap = {}> {
    doSomething<K extends keyof TMap>(
        key: K,
        datum: TMap[K] extends undefined ? omitted | undefined : TMap[K]
    ) {
        console.log({ key, datum });
    }
}

const foo = new Foo<{ one: string, two: undefined }>();
foo.doSomething("one", "a");        // valid
foo.doSomething("one");             // ts(2554) as expected
foo.doSomething("two", undefined);  // valid
foo.doSomething("two");             // valid

💻 Use Cases

  1. 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.

  1. 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.

class Foo<TMap = {}> {
    doSomething<K extends keyof TMap>(key: K, datum?: TMap[K]) {
        console.log({ key, datum });
    }
}

const foo = new Foo<{ one: string, two: undefined }>();
foo.doSomething("one", "a");
foo.doSomething("one"); // no ts(2554)
  1. 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.

class Foo<TMap = {}> {
    doSomething<K extends keyof TMap>(
        key: K,
        ...data: TMap[K] extends undefined ? [] | [undefined] : [TMap[K]]
    ) {
        const [ datum ] = data;
        console.log({ key, datum });
    }
}

Skateside avatar Feb 23 '24 22:02 Skateside

This sounds identical to #12400?

RyanCavanaugh avatar Feb 23 '24 22:02 RyanCavanaugh

@RyanCavanaugh yeah, I think it is. I must have missed that one when I searched for issues - I'll close this ticket since uts just a duplicate

Skateside avatar Feb 25 '24 06:02 Skateside