microsoft / microsoft/TypeScript

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

Open
#56,825 3 comments 3 reactions 1 assignee View on GitHub

@PranavSenthilnathan is already working on this.

Since Mar 21, 2024.

Experience Enhancement Rescheduled Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.