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

Poor error message elaborations for array-like types #30839

Closed
DanielRosenwasser opened this issue Apr 10, 2019 · 1 comment · Fixed by #30916
Closed

Poor error message elaborations for array-like types #30839

DanielRosenwasser opened this issue Apr 10, 2019 · 1 comment · Fixed by #30916
Labels
Domain: Error Messages The issue relates to error messaging Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Experience Enhancement Noncontroversial enhancements Fixed A PR has been merged for this issue Help Wanted You can do this

Comments

@DanielRosenwasser
Copy link
Member

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...

@DanielRosenwasser DanielRosenwasser added Experience Enhancement Noncontroversial enhancements 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 labels Apr 10, 2019
@jack-williams
Copy link
Collaborator

Fix up: #30916

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". Experience Enhancement Noncontroversial enhancements Fixed A PR has been merged for this issue Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants