Skip to content
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

Forced to pass undefined when a method accepts void or undefined #19259

Closed
inad9300 opened this issue Oct 17, 2017 · 7 comments
Closed

Forced to pass undefined when a method accepts void or undefined #19259

inad9300 opened this issue Oct 17, 2017 · 7 comments
Labels
Duplicate An existing issue was already created

Comments

@inad9300
Copy link

inad9300 commented Oct 17, 2017

If a method accepts void as the type of one of its arguments, passing undefined to it should not be explicitly required, in my opinion, as the method will receive undefined anyway if no argument is provided.

My use case is as follows:

class C<T> {

    f(a: T) {}
}

const c = new C<void>() // Or: new C<undefined>()
c.f(undefined) // OK.
c.f() // Expected 1 arguments, but got 0.

Might be an uncommon case, but what I would like to effectively indicate from the the user of class C is the need or not to provide the argument a. In other words, the method f() of C should force you to send an argument to it if the type is different than void (so an optional argument doesn't work, unless there is a way to condition this to the argument type), but also allow you to say "I don't need an argument in f()" (and using a default type, e.g. any wouldn't be very type-safe, of course, so I would like to avoid this option).

What is the best possible way to achieve this for the time being? If none, does my proposal make sense?

@mhegazy
Copy link
Contributor

mhegazy commented Oct 17, 2017

I think void here is the wrong type. only use void in return type positions for functions to indicate the lack of a return value.

I think there is merit in general in allowing parameters that accept undefined to be skipped in a call under --strictNullChecks, in other words, treat f(a: number|undefined) as f(a?: number).

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Oct 17, 2017
@inad9300
Copy link
Author

I think you expressed it perfectly; that's it: T | undefined should be semantically equivalent to T?. And probably, if not under strict mode, T | undefined | null should be equivalent to T?.

@inad9300 inad9300 changed the title Forced to pass undefined when a method accepts void Forced to pass undefined when a method accepts void or undefined Oct 18, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Oct 18, 2017

No just T|undefined. Null has a different value.

@inad9300
Copy link
Author

Well, you are the expert :)

@mhegazy
Copy link
Contributor

mhegazy commented Oct 20, 2017

looks like a duplicate of #12400

@mhegazy mhegazy added Duplicate An existing issue was already created and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Oct 20, 2017
@inad9300
Copy link
Author

So it seems indeed.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 6, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Nov 6, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants