Skip to content

Instantly share code, notes, and snippets.

@seivan
Last active April 25, 2020 22:14
Show Gist options
  • Save seivan/3bc5ee523941c744c30e1b05f49d452c to your computer and use it in GitHub Desktop.
Save seivan/3bc5ee523941c744c30e1b05f49d452c to your computer and use it in GitHub Desktop.
Deep Read Only Typescript 3.8
export type DeepReadOnly<P> =
P extends undefined | null | boolean | string | number | Function
? P :
P extends Array<infer T>
? ReadonlyArray<DeepReadOnly<T>> :
P extends Map<infer K, infer V>
? ReadonlyMap<DeepReadOnly<K>, DeepReadOnly<V>> :
P extends Set<infer M>
? ReadonlySet<DeepReadOnly<M>> :
{ readonly [K in keyof P]: DeepReadOnly<P[K]> }
export type DRO<P> = DeepReadOnly<P>
import {DeepReadOnly} from "deep-read-only"
const globalStore: DeepReadOnly<TodoStore> = new TodoStore()
export const App = (props: DeepReadOnly<{ store: TodoStore }>) => {
return <button onClick={
() => { props.store.insert("Hey") }
}>
{props.store.todos.length > 0 && props.store.todos[0].text}
</button>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment