Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(types): make Snapshot<T> use read-only collections #850

Merged
merged 5 commits into from
Feb 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions src/vanilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,22 @@ type Op =
type Listener = (op: Op, nextVersion: number) => void

type Primitive = string | number | boolean | null | undefined | symbol | bigint

type SnapshotIgnore =
| Date
| Map<any, any>
| Set<any>
| WeakMap<any, any>
| WeakSet<any>
| AsRef
| Error
| RegExp
| AnyFunction
| Primitive
type SnapshotIgnore = Date | AsRef | Error | RegExp | AnyFunction | Primitive
type Collection = Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any>
type Readonly<T extends Collection> = Omit<
T,
'set' | 'add' | 'delete' | 'clear'
>

type Snapshot<T> = T extends SnapshotIgnore
? T
: T extends Promise<unknown>
? Awaited<T>
: T extends object
? { readonly [K in keyof T]: Snapshot<T[K]> }
: T
: T extends Collection
? Readonly<T>
: T extends object
? { readonly [K in keyof T]: Snapshot<T[K]> }
: T

/**
* This is not a public API.
Expand Down
Loading