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

Do not substitute string literals with string when reporting errors #56844

Closed
6 tasks done
erheron opened this issue Dec 21, 2023 · 4 comments
Closed
6 tasks done

Do not substitute string literals with string when reporting errors #56844

erheron opened this issue Dec 21, 2023 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@erheron
Copy link

erheron commented Dec 21, 2023

πŸ” Search Terms

error box, string literal, type assignability

βœ… Viability Checklist

⭐ Suggestion

Do not coerce string literal types to a "general" string type when checking assignability and reporting errors.

πŸ“ƒ Motivating Example

Given a code like this:

type S = "Literal type";
declare const s: S;

function acceptsNumber(arg: number){}

acceptsNumber(s);

In TS v3.9.7 the last line would trigger an error Argument of type '"Literal type"' is not assignable to parameter of type 'number'. The string literal type would be presented as it is, and we were using it a lot, primarily to craft helpful error messages in some corner cases of conditional types.

However, since TS v4.0.5 the error changes to the following: Argument of type 'string' is not assignable to parameter of type 'number'.. That's a correct error, but we consider it to be less helpful than the previous one.

πŸ’» Use Cases

1. What do you want to use this for?

More helpful error messages in conditional types (e.g. telling programmers that "Hey, you probably did X and Y wrong")

2. What shortcomings exist with current approaches?

Instead of seeing a message in the error box (preview), people have to open the declaration file and understand how the conditional type is constructed

3. What workarounds are you using in the meantime?

Instead of using a string literal with "Error text", using a named type e.g. interface OpenThisError { message: "Error Text" }

@jcalz
Copy link
Contributor

jcalz commented Dec 21, 2023

Presumably this request is a workaround for the lack of #23689 / #40468. Or is there a use case that doesn't involve emulating throw types?

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Dec 21, 2023
@RyanCavanaugh
Copy link
Member

The widening from literal to string in the error message is intentional, since it's clearer that the base primitive type itself is wrong.

@erheron
Copy link
Author

erheron commented Dec 22, 2023

Thanks for such a quick response folks! ❀️
If that's working as intended, shall we close this issue, or shall we keep this open as an indicator of community interest in #23689?

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 25, 2023
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