Skip to content

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

@jbrazeau

Description

@jbrazeau

πŸ”Ž 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

https://www.typescriptlang.org/play?ts=5.3.3#code/MYewdgzgLgBAYgJQKoEkAqBlGBeGBtAKBhgCIBDABwoBsBTEgGiNICMyx2zHmSXqzgAaxa0ATqICe3YiWAALMZOmkQo9gHN6TGWogUR4qUwC6MMhBihIUANwECAegcwAIrQBmASzC0zMKBIUvgKgogAm3ur+IP4KMNCikWbiZBIEAUHwogCunlAWuAAUGbQg7vDI6BgAlHhg2QC2BsZ2jgBUbURtMBhyINnUYTAiMGEe3rRD5jAA5KK0ZGHg1BJZuVB4xjMwhQ2eEBBJ84vLq4K0EgDuqmHVAHRd-hDAMO5kntQWl3lyMACsdwAjHcAGwwVT-O4AJmhj3yLyWtAgYBmsDU+2ClhADQoHzIUE84Bgigh3ygvwBAGY7pTwaIYGBPOo5FAVjt8TA6OZYFAkbyhmSKXcACx3AAMAFoxgA3aFiqHCsWAqFi6pdBwEKzQGAIACiLgA+ohUJgAFxrPIQTY4fCyBSGRikXT6RRSUzTLW2exOGAAQQsesNxqqMH2DJAaIWSzAKwYMEuvmA7GJ0toYBgFGyEF+0rI1GySNDYCgMXJwRSq2Kcg5YdoAA8gsB+Y5nCXhr5jtG2dMEzAs5NYonwNrzlcbkXORMYABiQEADjVgaNlUwd0z2cK5CodBI1VaPoAColi4PkmpVlYoO9GWAomWzNvfABCTXDkB0O7UEDqQpL4OYPcCCAA

πŸ’» 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

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions