Skip to content

Change else clause to never in ReturnType #44669

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

Closed
umireon opened this issue Jun 19, 2021 · 4 comments
Closed

Change else clause to never in ReturnType #44669

umireon opened this issue Jun 19, 2021 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@umireon
Copy link

umireon commented Jun 19, 2021

lib Update Request

Configuration Check

My compilation target is es5 and my lib is the default.

Missing / Incorrect Definition

The else clause of ReturnType is never reached because of the constraint so that it is reasonable to be never.

Sample Code

type T10 = ReturnType<() => string>;  // string
>T10 : string

type T11 = ReturnType<(s: string) => void>;  // void
>T11 : void
>s : string

type T12 = ReturnType<(<T>() => T)>;  // {}
>T12 : unknown

type T13 = ReturnType<(<T extends U, U extends number[]>() => T)>;  // number[]
>T13 : number[]

type T14 = ReturnType<typeof f1>;  // { a: number, b: string }
>T14 : { a: number; b: string; }
>f1 : (s: string) => { a: number; b: string; }

type T15 = ReturnType<any>;  // unknown
>T15 : unknown // changed from any

type T16 = ReturnType<never>;  // never
>T16 : never

type T17 = ReturnType<string>;  // Error
>T17 : never // changed from any

type T18 = ReturnType<Function>;  // Error
>T18 : never // changed from any

type T19<T extends any[]> = ReturnType<(x: string, ...args: T) => T[]>;  // T[]
>T19 : T[]
>x : string
>args : T

Documentation Link

https://www.typescriptlang.org/docs/handbook/utility-types.html#returntypetype

Changeset

I already made a changeset. I didn't know that a backlog issue is required to open a pull request.

https://github.com/microsoft/TypeScript/compare/main...umireon:returntype-never?expand=1

Duplicates

A possible duplicate is #39584 but nothing is discussed there.

@Zzzen
Copy link
Contributor

Zzzen commented Jun 20, 2021

any makes sense here. It reduces noise when a type error is encountered. If it is replaced with never, there will be a lot of Property 'xxx' does not exist on type 'never'., which may shadow the real error Type 'xxx' does not satisfy the constraint '(...args: any) => any'.

type SomeType = string

// Type 'string' does not satisfy the constraint '(...args: any) => any'.(2344)
let foo: ReturnType<SomeType>

// Property 'toLowerCase' does not exist on type 'never'.
foo.toLowerCase()

@MartinJohns
Copy link
Contributor

MartinJohns commented Jun 20, 2021

I don't really see the purpose of this change. The only benefit you have is receiving unknown when the type argument is any, and this is questionable at most (and breaking).

Additionally I think any makes more sense in the case of type errors. The never type explicitly says that the function does not return. But due to the type error you don't get a valid result, so you can't say this won't return and defaulting to any makes more sense.

@umireon
Copy link
Author

umireon commented Jun 20, 2021

I was really convinced.
I understood that never is for some statements of function execution and not for type errors.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jun 21, 2021
@umireon umireon closed this as completed Jun 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants