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

Improve diagnostic when string from property is not assignable to string literal. #26413

Open
ghost opened this issue Aug 13, 2018 · 3 comments
Labels
Domain: Error Messages The issue relates to error messaging Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@ghost
Copy link

ghost commented Aug 13, 2018

TypeScript Version: 3.1.0-dev.20180810

Code

interface IceCreamOptions {
    readonly cherry: "yes" | "no"
}
declare function iceCream(options: IceCreamOptions): void;
const options = { cherry: "yes" };
iceCream(options);

Expected behavior:

Error message recommending to add a type annotation to options. This should be detectable because the source of the failing property is a PropertyAssignment in an object literal being assigned to a variable. (If there is no variable assignment, we could still recommend using ``"yes" as "yes"although that isn't as pretty.) Could also come with a codefix. Note that in JS the fix is slightly different: put/** @type {IceCreamOptions} /` before the `const` or use `/* @type {"yes"} */ ("yes")`.

Actual behavior:

Error messages says Type 'string' is not assignable to type '"yes" | "no"'. with no information on how to fix it.

@ghost ghost added Suggestion An idea for TypeScript Help Wanted You can do this Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Domain: Error Messages The issue relates to error messaging labels Aug 13, 2018
@Kingwl
Copy link
Contributor

Kingwl commented Aug 28, 2018

add "yes" as "yes" is soooooooooooooooooooooooo ugly
are there any other way to declare the string literal type?

@RyanCavanaugh
Copy link
Member

@Kingwl discussion at #10195

@Kingwl
Copy link
Contributor

Kingwl commented Mar 22, 2019

I'm going to improve this one, but I found the type of diagnostic is DiagnosticMessage or DiagnosticMessageChain that without position info, and I cannot get the un-assignable props in the object literal simply,

As the problem,i'd like to replace DiagnosticMessageChain with DiagnosticRelatedInformation chain
for isRelatedTo and that will hold the position info

ping @RyanCavanaugh

Am I missing anything?

@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Aug 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Error Messages The issue relates to error messaging Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants