Skip to content

ReturnType<> broken with never in parameter #33457

@AnyhowStep

Description

@AnyhowStep

TypeScript Version: 3.5.1

Search Terms:

parameter, never, return type inference

Code

type NeverF = (arg : never) => void;

//"y"
type Extends = NeverF extends (...args : any) => any ?
    "y" :
    "n"
;

type TsReturnType<T extends (...args : any) => any> =
    T extends (...args : any) => infer R ?
    R :
    "wtf"
;

//Expected: void
//Actual  : "wtf"
type r = TsReturnType<NeverF>;

//Expected: void
//Actual  : any
type r2 = ReturnType<NeverF>;

Expected behavior:

Both should resolve to void

Actual behavior:

Both resolve to the false branch

Playground Link: Playground

Related Issues:

Did a search for "parameter never in:title" and found nothing


I'm using a function with a parameter of type never because I only care about the return type. And I do not want the user to invoke the function explicitly.

Given the following,

const query2 = myQuery.map((row) => {
  return {
    ...row,
    x : Math.random(),
  };
});

The type of query2 should now be something like,

{
  /*snip*/
  mapDelegate : (row : never, connection : /*snip*/) => (
    { /*snip typeof row*/, x : number }
  ),
  /*snip*/
}

Then,

const query3 = query2.map((row) => {
  return {
    ...row,
    x : row.x + 3.141,
    y : "someNewValue",
  };
});

The type of query3 should now be something like,

{
  /*snip*/
  mapDelegate : (row : never, connection : /*snip*/) => (
    { /*snip typeof row*/, x : number, y : string }
  ),
  /*snip*/
}

I'm also using a never parameter because I want to save on Emit time. It sounds funny but literally every bit of text I shave off helps with compile times.

For large queries, the type of the row parameter could be huuuuuuggeeeee. Like, 100+ lines long.

So,

  1. Only care about the return type
  2. Not meant to be invoked externally
  3. Save on emit time

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions