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

type-challenges-solutions/en/medium-return-type #277

Open
utterances-bot opened this issue Nov 1, 2022 · 4 comments
Open

type-challenges-solutions/en/medium-return-type #277

utterances-bot opened this issue Nov 1, 2022 · 4 comments

Comments

@utterances-bot
Copy link

Get Return Type

This project is aimed at helping you better understand how the type system works, writing your own utilities, or just having fun with the challenges.

https://ghaiklor.github.io/type-challenges-solutions/en/medium-return-type.html

Copy link

zhaoyao91 commented Nov 1, 2022

Why you fallback to T instead of never?

like this

type MyReturnType<T> = T extends (...args: any[]) => infer R ? R : never;

@ghaiklor
Copy link
Owner

ghaiklor commented Nov 1, 2022

@zhaoyao91 there is no reason, it just a preferable choice for me.

Copy link

A couple of questions:

  1. How is it valid that T extends (...args: any[]) covers the cases where no parameters are passed in? it seems like we would be passing it undefined rather than an empty array.

  2. Regarding the previous comments, how is 'never' interchangeable with 'T'? Like what does it mean to return T here? and does returning 'never' just mean that if they try to use this type and pass in something other than a function, it will throw a TS error?

@ghaiklor
Copy link
Owner

@Brysonmk1984

how is 'never' interchangeable with 'T'?

It's not in this context. As I said, it was just a choice, not based on anything. Feel free to return never if you want.

does returning 'never' just mean that if they try to use this type and pass in something other than a function, it will throw a TS error?

never is a type that says some situation X must not happen. You can read more about it here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants