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

Confusing error message with incompatible readonly tuple types #35060

Closed
jcalz opened this issue Nov 12, 2019 · 2 comments · Fixed by #35065
Closed

Confusing error message with incompatible readonly tuple types #35060

jcalz opened this issue Nov 12, 2019 · 2 comments · Fixed by #35065
Assignees
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Fixed A PR has been merged for this issue Good First Issue Well scoped, documented and has the green light Help Wanted You can do this

Comments

@jcalz
Copy link
Contributor

jcalz commented Nov 12, 2019

TypeScript Version: 3.8.0-dev.20191112

Search Terms: readonly, array, error, mutable, tuple,

Code

const x: readonly [] = null! as readonly [0];
/*
The type 'readonly [0]' is 'readonly' and cannot be 
assigned to the mutable type 'readonly []'.
 Types of property 'length' are incompatible.
  Type '1' is not assignable to type '0'.(4104) 
*/

const y: readonly [1] = null! as readonly [0];
/* 
The type 'readonly [0]' is 'readonly' and cannot be 
assigned to the mutable type 'readonly [1]'.
 Type '0' is not assignable to type '1'.(4104)
*/

Expected behavior:
Errors saying that readonly [0] cannot be assigned to readonly [] or readonly [1] due to incompatible lengths or element types.

Actual behavior:
Contradictory error messages about absurdities like "the mutable type readonly[]", suggesting that some readonly arrays are not, in fact, readonly, and generally prompting developers to question their own sanity at a slightly greater rate than usual.

It looks like #30916, the fix for #30839, went a bit too far in making the error messages for tuple mismatches human-friendly. If both the source and target types are assignable to ReadonlyArray, then the error message should probably not mention mutability at all and just say the types are incompatible. Further elaborations about length or index signature value mismatches should stay how they are now.

Thanks to @eyelidlessness for noticing this.

Playground Link

Related Issues:
#30839: Poor error message elaborations for array-like types
#30916: Elaborate array and tuple relation errors

@jcalz jcalz changed the title Confusing error message with incompatible readonly array types Confusing error message with incompatible readonly tuple types Nov 12, 2019
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Nov 12, 2019
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.8.0 milestone Nov 12, 2019
@DanielRosenwasser DanielRosenwasser added Domain: Error Messages The issue relates to error messaging Good First Issue Well scoped, documented and has the green light Help Wanted You can do this labels Nov 12, 2019
quin pushed a commit to quin/TypeScript that referenced this issue Nov 13, 2019
DanielRosenwasser pushed a commit that referenced this issue Nov 15, 2019
* Fix 35060

* Refactor and check ro-array to tuple case

* Lint
@DanielRosenwasser
Copy link
Member

Thanks @jack-williams!

@DanielRosenwasser DanielRosenwasser added the Fixed A PR has been merged for this issue label Nov 15, 2019
@jcalz
Copy link
Contributor Author

jcalz commented Nov 17, 2019

Looks good, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Fixed A PR has been merged for this issue Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Projects
None yet
3 participants