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

14.5-literal-inference.explainer.ts has misleading statements #32

Open
Maksandre opened this issue Mar 23, 2024 · 0 comments
Open

14.5-literal-inference.explainer.ts has misleading statements #32

Maksandre opened this issue Mar 23, 2024 · 0 comments

Comments

@Maksandre
Copy link

Maksandre commented Mar 23, 2024

You have a comment in the 14.5-literal-inference.explainer.ts file:

// When returning the value only, it infers
// the literal type
const returnsValueOnly = <T>(t: T) => {
  return t;
};

const result = returnsValueOnly("a");
//    ^?

This explanation is a bit misleading. In your example, it returns literal type not because of how your function is declared but because of how it is called. In other words, it is returned literal because "a" parameter is interpreted as literal.

For example, if you pre-declare the function's argument as let the type of return value will be a string:

Screenshot 2024-03-23 at 15 51 28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant