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

ReturnType<> broken with never in parameter (please revisit) #35432

Closed
AnyhowStep opened this issue Dec 1, 2019 · 5 comments
Closed

ReturnType<> broken with never in parameter (please revisit) #35432

AnyhowStep opened this issue Dec 1, 2019 · 5 comments
Labels
Unactionable There isn't something we can do with this issue

Comments

@AnyhowStep
Copy link
Contributor

TypeScript Version: 3.7.2

Search Terms:

parameter, never, ReturnType

Code

The AnyFunction type is found here:

export type AnyFunction = (...args: never[]) => void;

export type AnyFunction = (...args: never[]) => void;
//Expected: void
//Actual: any
type x = ReturnType<AnyFunction>;

Expected behavior:

x should be void

Actual behavior:

x is any

Playground Link:

Playground

Related Issues:

Original Issue:
#33457

PR to fix ReturnType<>:
#33496

@jack-williams tracking the never and rest params issue:
#33495

@RyanCavanaugh RyanCavanaugh added the Unactionable There isn't something we can do with this issue label Jan 8, 2020
@RyanCavanaugh
Copy link
Member

I don't think it should be intended that "never" in a parameter breaks ReturnType

I disagree and don't see any case being made here. A function that expects a never parameter can't be invoked.

What problem is this causing for you?

@AnyhowStep
Copy link
Contributor Author

AnyhowStep commented Jan 8, 2020

The original issue I linked to actually explains the case. But I'll just explain again.

The use case is precisely that I don't want the function to be invoked externally, but I want to preserve the return type information only.

interface IQuery<DataT extends QueryData> {
  someNonGenericProp : string;
  mapDelegate : DataT["mapDelegate"];
}

An object of the above interface is returned after using a query builder. The mapDelegate is not intended to be invoked directly and the arguments are a super large type, anyway, that will make emit times super slow (this is the whole reason I'm trying to erase argument type information).

The return type information is important, the argument type information must be erased (faster emit, not meant to be invoked)


This IQuery interface is passed to a function that, internally, "knows" how to call the mapDelegate correctly.

This function needs to know the return type of mapDelegate that has had its argument types erased and converted to never


What problem is this causing for you?

At the moment? I have a workaround

But each workaround I use just makes the code harder to read and understand, a few weeks down the road.

I didn't realize this was the current behavior; that never in arguments would break the return type inference.

I only caught it because I wrote some compile time tests and noticed it. Then I had to figure out what was causing it, then I had to figure out a reasonable workaround.

@AnyhowStep
Copy link
Contributor Author

AnyhowStep commented Jan 8, 2020

The TS code base uses never in arguments for functions that it does not want to invoke directly, but knows that some other thing knows how to invoke it properly.

I don't think it's unreasonable to expect ReturnType to work properly, for these functions (like for my use case)

@jack-williams
Copy link
Collaborator

A function of type (...args: never[]) => void is callable: the type never[] is the type of empty lists so the function should be no different to (...args: []) => void.

I think the problem is already covered in existing issues and boils down to

  1. Whether or not the proposal in never rest type not assignable to complex rest type #33495 should be accepted
  2. And assuming (1), changing the definition of ReturnType to use (...args: never) => infer R instead.

@AnyhowStep
Copy link
Contributor Author

AnyhowStep commented Jan 8, 2020

In my PR/workaround, I'm using,
https://github.com/AnyhowStep/TypeScript/blob/15737c267e536bb373b8378628428fc831868a02/src/lib/es5.d.ts#L1469

type ReturnType<T extends (...args: any) => any> = T extends (...args: never) => infer R ? R : any;
  • any for the type parameter
  • never for the inference

Because never in the type parameter broke some tests.

Playground


Using never[] also breaks stuff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Unactionable There isn't something we can do with this issue
Projects
None yet
Development

No branches or pull requests

3 participants