question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Void parameter still required when type extends generic

See original GitHub issue

TypeScript Version: 3.2.2

Search Terms: void parameter type extends generic Code

function works1(n: number, b: void) { }

works1(12);

function works2(n: number, b: 1 extends 1 ? void : number) { }

works2(12);

function fails<T>(n: number, b: T extends 1 ? void : number) { }

fails<2>(12, 2);  // works, requires both params
fails<1>(12);  // fails, requires 2 parameters even though second param is void

Expected behavior: That I can ignore the second parameter since its void

Actual behavior: Ts tells me I am missing a parameter

https://www.typescriptlang.org/play/index.html#src=function works1(n%3A number%2C b%3A void) { } works1(12)%3B function works2(n%3A number%2C b%3A 1 extends 1%3F void %3A number) { } works2(12)%3B function fails<T>(n%3A number%2C b%3A T extends 1%3Fvoid%3Anumber) { } fails<2>(12%2C2)%3B %2F%2Fworks%2C requires both params fails<1>(12)%3B %2F%2F requires 2 parameters even though second param is void

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:19
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
dragomirtitiancommented, Dec 14, 2019

@parzh You can get the parameter names back (mostly) if you don’t mind writing a bit more:

declare function fn<Value extends 1 | 2>(...args: Value extends 1 ?  Parameters<(x: number) =>void> : Parameters<(y:number, name: string) => void>): void;

fn<1>(42);
fn<1>(42, "hello world"); // Error: expects 1 argument

fn<2>(42); // Error: expects 2 arguments
fn<2>(42, "hello world");

Playground Link

1reaction
parzhcommented, Dec 13, 2019

Variable number of function parameters can still be achieved using tuple types (although, parameter identifiers in IntelliSense will be lost unfortunately):

declare function fn<Value extends 1 | 2>(...args: Value extends 1 ? [number] : [number, string]): void;

fn<1>(42);
fn<1>(42, "hello world"); // Error: expects 1 argument

fn<2>(42); // Error: expects 2 arguments
fn<2>(42, "hello world");

Link to playground.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Understanding generic parameters with void return types
I'm reviewing the source code for JDK classes. The following method signature has me confused: public static <T extends Comparable<? super ...
Read more >
Restrictions on Generics (The Java™ Tutorials > Learning the ...
Because the Java compiler erases all type parameters in generic code, you cannot verify which parameterized type for a generic type is being...
Read more >
Generics: in, out, where | Kotlin Documentation
Classes in Kotlin can have type parameters, just like in Java: ... { void addAll(Collection<? extends E> items); }. The wildcard type argument...
Read more >
Resolving generics in TypeScript
In this article you will find some useful examples of typing factory functions which requires you to use several generics with constraints ...
Read more >
Constraints on type parameters - C# Programming Guide
The usefulness of type parameters as constraints with generic classes is limited because the compiler can assume nothing about the type ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found