DEV Community

Discussion on: Deep Readonly Generic in Typescript

Collapse
 
danielearwicker profile image
Daniel Earwicker • Edited

The Readonly<T[K]> for primitives is unnecessary because those types don't have any mutable structure - you can't edit what is stored inside a number, string or symbol object. (Also you missed out boolean.) For all primitives you can just map to T[K].

You're already making the properties (that hold these values) read-only by wrapping the whole type in Readonly on the first line, so that's enough.

(If you wrap them with Readonly unnecessarily then TS can generate spurious type errors.)

Collapse
 
bwca profile image
Volodymyr Yepishev

Excellent point, thanks Daniel! 😁