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.

Generic type sometimes returns never instead of actual template parameter type

See original GitHub issue

TypeScript Version: 3.6+

Search Terms: enum, extends, never, template parameters

Code

enum FL4 { Absurd, False, True, Unknown }

type Classify< Left, Right > = Left extends Right

	? Right extends Left
		? [ Left , '==', Right ]
		: [ Left , '<:', Right ]
	
	: Right extends Left
		? [ Left, ':>', Right ]
		: [ Left , '!=', Right ]

Actual at 3.6+

type Absurd_is_never_wtf = Assert<
    Classify< FL4.Absurd, 0 >,
    [ never, '<:', 0 ]
>

type One_is_never_wtf = Assert<
    Classify< FL4.Absurd, 1 >,
    [ FL4.Absurd, ':>', never ]
>

Expected and before 3.6

type Absurd_is_Absurd = Assert<
    Classify< FL4.Absurd, 0 >,
    [ FL4.Absurd, '==', 0 ]
>

type One_is_One = Assert<
    Classify< FL4.Absurd, 1 >,
    [ FL4.Absurd, ':>', 1 ]
>
```

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
MaximeKjaercommented, Mar 12, 2021

We encountered this issue over at socketio/socket.io#3833. Here’s another minimized bug reproduction:

enum Enum {
  NAME = "name",
  WRONG_NAME = "wrong name"
}
type Name = "name";
type NameOrNull<N> = N extends Name ? N : null;

// Sanity checks:
type test1 = NameOrNull<"name">; // returns type `"test"`
type test2 = NameOrNull<"wrong name">; // returns type `null`
type test3 = Enum.NAME extends Name ? Enum.NAME : null // returns type `Enum.NAME`
type test4 = NameOrNull<Enum.WRONG_NAME>; // returns type `null`

// Unexpected behavior:
type test5 = NameOrNull<Enum.NAME>; // returns type `never`, but expected type `Enum.NAME`
0reactions
jcalzcommented, May 23, 2022

So then this is a duplicate/related to #21998, I think. Why can’t we have the intersection of an enum and the wider literal it comes from just be the enum? Especially for string enums, which doesn’t have the bitflag craziness to deal with?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript Function with Generic Return Type - Stack Overflow
To reiterate: when you have a generic function, the generic type parameters are specified by the caller, not by the implementer.
Read more >
Handbook - Unions and Intersection Types - TypeScript
How to use unions and intersection types in TypeScript. ... let you compose or combine existing types instead of creating them from scratch....
Read more >
Function template - cppreference.com
Two function templates with the same return type and the same parameter list are distinct and can be distinguished with explicit template argument...
Read more >
Effective Dart: Design
A boolean name should never sound like a command to tell the object to do something, ... At compile time, the compiler looks...
Read more >
Java Generics Example Tutorial - Generic Method, Class ...
The wildcard can be used as the type of a parameter, field, or local variable and sometimes as a return type. We can't...
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 Reddit Thread

No results found

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