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

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

Closed
jbrazeau opened this issue Jan 20, 2024 · 7 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@jbrazeau
Copy link

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

@fatcerberus
Copy link

I'm thinking this might be fallout from #55229

@Andarist
Copy link
Contributor

Andarist commented Jan 20, 2024

Fallout sounds like this wasn't intended there but, from what I can tell, this was a deliberate change introduced in this PR. While the PR's title might only mention type parameters and such - the change itself was broader, it affected cases like the one below and the team knew about it:

const arr2 = [1, 2, 3] as const satisfies unknown[] // ok since that PR

@fatcerberus
Copy link

Fallout sounds like this wasn't intended there but, from what I can tell, this was a deliberate change introduced in this PR.

I wasn't sure since the stated purpose of the PR according to Anders was just to improve behavior for const type parameters and even when he brought up contextual typing, it was in the context of generic types:

We now infer a mutable array type when an [...] as const array literal is contextually typed by a type parameter with a constraint that includes a mutable array type.

He specifically calls out contextual typing via "a constraint that includes a mutable array type", not simply "a mutable array type" (the actual behavior clearly includes the latter). At no point in that PR could I find an acknowledgement of this behavior for non-generic contextual typing, but maybe I missed it...

@Andarist
Copy link
Contributor

Andarist commented Jan 20, 2024

it has been mentioned in the design meeting notes that this change wasn't restricted to const type parameters. I agree that the PR's description doesn't make it particularly obvious but it feels like this change in the behavior wasn't accidental.

@ahejlsberg
Copy link
Member

This is behaving as intended. I agree #55229 should have mentioned this applies to as const as well.

@ahejlsberg ahejlsberg added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jan 21, 2024
@fatcerberus
Copy link

@ahejlsberg Well, you do mention as const later in the thread, but kind of imply that’s only for contextual typing by a type parameter.

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants