[TypeScript] Deeply mark all the properties of a type as read-only in TypeScript
We will look at how we can use mapped types, conditional types, self-referencing types and the “infer” keyword to create a reusable generic type that traverses down the properties of an object and marks of all them as read-only. This is especially useful when used with Redux, to ensure our whole state tree is marked as read-only and immutable.
?
For example we have complex interface:
interface IRootState {userId: string;showCompletedOnly: boolean;todoTypes: string[];todos: ITodo[];iconGrid: string[][]; }interface IEmail {from: string;to: string[];body: string; }interface ITodo {isCompleted: boolean;text: string;linkedEmail: IEmail; }?
If we want to add readonly to all the props of IRootState. We want to do it automaticlly.
type DeepReadonlyObject<T> = { readonly [K in keyof T]: DeepReadonly<T[K]> };type DeepReadonly<T> = T extends (infer E)[] ?ReadonlyArray<DeepReadonlyObject<E>> :T extends object ? DeepReadonlyObject<T> :T; type IReadonlyRootState = DeepReadonly<IRootState>;轉載于:https://www.cnblogs.com/Answer1215/p/10321465.html
總結
以上是生活随笔為你收集整理的[TypeScript] Deeply mark all the properties of a type as read-only in TypeScript的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 守卫者的挑战(guard)
- 下一篇: 数位 DP 入门 (不要 62+wind