-
Notifications
You must be signed in to change notification settings - Fork 12.9k
const array to non readonly array cast compilation does not raise an error with [email protected] or [email protected] (like it does with [email protected] or [email protected])Β #57107
Copy link
Copy link
Closed as not planned
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
Description
π Search Terms
is:issue is:open const readonly array 5.3.3
π Version & Regression Information
- This changed between versions 5.2.2 and 5.3.3
β― Playground Link
π» Code
const FRUITS = [
"apple",
"banana",
"blackberry",
"cherry",
"orange",
"raspberry",
] as const;
// Define a type according to the string array
type Fruits = (typeof FRUITS)[number];
/**
* Should be defined as 'readonly Fruit[]' (missing readonly keyword).
* tsc fails with 5.1.6 or 5.2.2
* tsc doesn't raise a compilation error with 5.3.3 or nightly (at least tested with 5.4.0-dev.20240120)
*/
const RED_FRUITS: Fruits[] = ["cherry", "raspberry"] as const;
// As RED_FRUITS is not readonly, we can even push values into the array (that is expected
// to be readonly as we used the const keyword in line #18)
RED_FRUITS.push("apple");
// Print the array containing the apple !
console.log(RED_FRUITS);
π Actual behavior
With typescript 5.3.3 (or nightly, tested with 5.4.0-dev.20240120) the provided code compiles with no error.
π Expected behavior
The compilation with 5.3.3 (or with nightly, tested with 5.4.0-dev.20240120) should behave the same way as previous typescript releases (5.1.6, 5.2.2, ...) and raise a compilation problem at line 19 :
src/index.ts:19:7 - error TS4104: The type 'readonly ["cherry", "raspberry"]' is 'readonly' and cannot be assigned to the mutable type '("apple" | "banana" | "blackberry" | "cherry" | "orange" | "raspberry")[]'.
19 const RED_FRUITS: Fruits[] = ["cherry", "raspberry"] as const;
~~~~~~~~~~
Found 1 error in src/index.ts:19
Additional information about the issue
In the playground simply switch typescript version (5.1.6, 5.2.2, 5.3.3, nightly) to see the compilation problem appear / disappear.
Metadata
Metadata
Assignees
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug