镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Specialized error message when asking for a named object property on a tuple?  #56825

Description

🔍 Search Terms

named object property tuple type lookup

✅ Viability Checklist

⭐ Suggestion

Developers sometimes accidentally use [] to declare a type instead of {}. Later, when they try to access a property by string name, the error message doesn't indicate that it's a [] tuple instead of {} object. Can we get a specific error message for that example?

📃 Motivating Example

The following Label type alias seems to describe a reasonable tuple at first. But later on we see that the developer might have meant to describe an object type.

type Label = [
  text: string;
];

declare const label: Label;

label.text;
// Property 'text' does not exist on type 'Label'.

How about a specialized diagnostic indicating 'Label' is a tuple?

Property 'text' does not exist on type 'Label'.
  'Label' is a tuple type with numeric keys, not an object type with string keys.

...bikeshedding needed on that message 🤔

💻 Use Cases

  • When a developer meant to write an object type but used a tuple instead
  • When a developer misread a tuple type as an object type
  • When a developer is new to tuples and/or types, and confused the two's syntaxes

Example real-world developer pain point (posted with permission): https://twitter.com/yagiznizipli/status/1736762337716039967

Activity

  1. RyanCavanaugh commented on Dec 18, 2023

    @RyanCavanaugh
    Member

    This seems like a good idea. In theory I think we can go further and offer the index in the error message, if it exists (maybe we should only do it in that case?)

  2. fatcerberus commented on Dec 19, 2023

    @fatcerberus

    So that would be something on the order of

    'Label' is a tuple type. Did you mean 'label[0]`?
    

    Downside: This suggestion might be misleading, if the actual mistake the developer made was using [] instead of {} in the definition, as hinted in the Use Cases section of the OP.

  3. DanielRosenwasser commented on Mar 21, 2024

    @DanielRosenwasser
    Member

    Suggested error messages:

    This value has no property named '{0}', but a corresponding label exists. Access its value at index '{1}'.
    

    Related info on the label itself:

    The label '{0}' does not name a property, it only documents the contents at a position.
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Experience EnhancementNoncontroversial enhancementsRescheduledThis issue was previously scheduled to an earlier milestoneSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions