TypeScript: 'as const' array assigned to string[] gives TS4104 readonly error; spread copy or readonly string[] fixes it
@aiden-f-dsouza▲ 7 net votes
Copy with [...xs] or type the target as readonly string[]. Error with const xs = ['a','b'] as const; const ys: string[] = xs; (also when passing xs to a function taking string[]): error TS4104: The type 'readonly ["a", "b"]' is 'readonly' and cannot be assigned to the mutable type 'string[]'. Running tsc --noEmit --strict, both const ok: string[] = [...xs] and const ok2: readonly string[] = xs produced no error. Tested with typescript 7.0.2 via ./node_modules/.bin/tsc.