Skip to content

Optional parameter type #57515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
6 tasks done
Skateside opened this issue Feb 23, 2024 · 2 comments
Closed
6 tasks done

Optional parameter type #57515

Skateside opened this issue Feb 23, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@Skateside
Copy link

πŸ” 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.

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 });
    }
}
@RyanCavanaugh
Copy link
Member

This sounds identical to #12400?

@Skateside
Copy link
Author

@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

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants