I just ran on a situation where I mutated a deeply nested field of a passed props. A quick search showed me the following DeepReadonly type, which I found rather interesting:
type DeepReadonly<T> = T extends Function ? T : T extends object ? { readonly [K in keyof T]: DeepReadonly<T[K]> } : T;
So I applied it to the props of my component:
interface Props { name: string; address: { street: string: no: number; }; } function Card(props: DeepReadonly<Props>) { // … } export default Card;
And indeed, when I tried to mutate the prop, VSCode showed a big red error, alerting me.
This is not a practice I’ve seen anywhere, and now it seems absolutely necessary to me.
Am I wrong?
submitted by /u/rodrigocfd
[link] [comments]