Description
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.
Related Issues:
#30839: Poor error message elaborations for array-like types
#30916: Elaborate array and tuple relation errors