Skip to content

Poor error message elaborations for array-like types #30839

Description

@DanielRosenwasser
let point = [3, 4] as const;

function distanceFromOrigin([x, y]: [number, number]) {
    return Math.sqrt(x ** 2 + y ** 2);
}

distanceFromOrigin(point);
Argument of type 'readonly [3, 4]' is not assignable to parameter of type '[number, number]'.
  Type 'readonly [3, 4]' is missing the following properties from type '[number, number]': pop, push, reverse, shift, and 6 more.

image

This is bad to show to beginners. Here's a few things we could be doing better.

  1. If a source type is a ReadonlyArray (for all E), and a target type is an Array, then give a specialized error message that ReadonlyArrays can't be assigned to Arrays.

    A 'ReadonlyArray' cannot be assigned to an 'Array' because 'Array's can be mutated.
    
  2. If the source type is a readonly tuple, and the target is a plain tuple or array-like type...

    A 'readonly' tuple cannot be assigned to a mutable array-like type.
    
  3. If one side is a tuple type, and the other isn't array-like, just don't elaborate at all. Nobody cares about missing push, pop, and 6 other members...

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".Experience EnhancementNoncontroversial enhancementsFixedA PR has been merged for this issueHelp WantedYou can do this

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions