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

Inconsistent behaviour in (trying) to make function arguments optional via generics #60482

Open
JelleRoets-Work opened this issue Nov 12, 2024 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@JelleRoets-Work
Copy link

JelleRoets-Work commented Nov 12, 2024

πŸ”Ž Search Terms

"function argument optional via generic"

πŸ•— Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about "optional function arguments via generics"

⏯ Playground Link

Playground Link

πŸ’» Code

const f1 = (arg: void) => {};
const r1 = f1(); // no error

const f2 = <T = void>(arg: T) => {};
const r2 = f2(); // Expected 1 arguments, but got 0, An argument for 'arg' was not provided.

type F<T = void> = (arg: T) => void;
const f3: F = arg => {};
const r3 = f3(); // no error

const f4 = <T = undefined>(arg: T extends undefined ? void : T) => {};
const r4 = f4<undefined>(); // Expected 1 arguments, but got 0, An argument for 'arg' was not provided.

class Klass<T = void> {
  constructor(arg: T) {}
  f(arg: T) {
    return;
  }
}
const k = new Klass(); // Expected 1 arguments, but got 0, An argument for 'arg' was not provided.
const r5 = k.f(); // no error

πŸ™ Actual behavior

I would like to have / need some tools to control whether or not a function argument is optional based on context given via generics.

In most cases I still get the TS error Expected 1 arguments, but got 0, where I wouldn't expect an error.
But I could find some (edge) cases where it's working tho, so at least the behaviour is inconsistent (see the example above)

πŸ™‚ Expected behavior

So in case the type of a function argument resolves into void (via a generic type or via conditional resolution), it should be possible to call that function without arguments without getting a TS error.

Additional information about the issue

Partially related #29131

@MartinJohns
Copy link
Contributor

Duplicate of #29131.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 12, 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

3 participants