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

Multiple optional function arguments #2289

Closed
anilanar opened this issue Aug 19, 2016 · 3 comments
Closed

Multiple optional function arguments #2289

anilanar opened this issue Aug 19, 2016 · 3 comments

Comments

@anilanar
Copy link

anilanar commented Aug 19, 2016

declare fn(str?: string, num?: number): void

Test

fn(5);

Actual Result

fn(5);
   ^ number. This type is incompatible with
fn(str?: string, num?: number): void;
         ^^^^^^ string.

Expected Result
I assume it should not error for the following uses:

fn();
fn('str');
fn(5);
fn('str', 5);
@anilanar anilanar changed the title Multiple optional arguments Multiple optional function arguments Aug 19, 2016
@vkurchatkin
Copy link
Contributor

I think it works as expected. You need to add an overload for this to work

@anilanar
Copy link
Author

anilanar commented Aug 19, 2016

Shouldn't this be documented somewhere though (even if it works as expected)?

@samwgoldman
Copy link
Member

@vkurchatkin is right on both counts, this is working as designed and you might be looking for function overloads. You might also be looking for union types, i.e., function f(arg: number | string) { ... }.

Contributions to the docs are very welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants