Last active
April 25, 2020 22:14
-
-
Save seivan/3bc5ee523941c744c30e1b05f49d452c to your computer and use it in GitHub Desktop.
Deep Read Only Typescript 3.8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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