Skip to content

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

Open

Description

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 (@type) {IceCreamOptions} /` before the `const` or use `/* Type (@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.

Activity

  1. ghost added
    SuggestionAn idea for TypeScript
    on Aug 13, 2018
  2. ghost added
    Effort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".
    on Aug 13, 2018
  3. ghost added on Aug 13, 2018
  4. Kingwl commented on Aug 28, 2018

    @Kingwl
    Contributor

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

  5. RyanCavanaugh commented on Aug 28, 2018

    @RyanCavanaugh
    Member
  6. Kingwl commented on Mar 22, 2019

    @Kingwl
    Contributor

    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 Ryan Cavanaugh (@RyanCavanaugh)

    Am I missing anything?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: Error MessagesThe issue relates to error messagingEffort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions