Open
Description
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
:

Metadata
Metadata
Assignees
Labels
No labels