Skip to content

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

Open
@Maksandre

Description

@Maksandre

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions