Skip to content

Confusing error message with incompatible readonly tuple types #35060

Closed
@jcalz

Description

@jcalz

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

Metadata

Metadata

Labels

BugA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingFixedA PR has been merged for this issueGood First IssueWell scoped, documented and has the green lightHelp WantedYou can do this

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions