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

Add a type-checking fast path for primitive types #755

Merged
merged 1 commit into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions compat/pre-3.7/dist/immer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ type Tail<T extends any[]> = ((...t: T) => any) extends (
? TT
: []

type PrimitiveType = number | string | boolean

/** Object types that should never be mapped */
type AtomicObject =
| Function
Expand All @@ -13,11 +15,10 @@ type AtomicObject =
| Promise<any>
| Date
| RegExp
| Boolean
| Number
| String

export type Draft<T> = T extends AtomicObject
export type Draft<T> = T extends PrimitiveType
? T
: T extends AtomicObject
? T
: T extends Map<infer K, infer V>
? DraftMap<K, V>
Expand All @@ -34,7 +35,9 @@ interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}
interface DraftSet<V> extends Set<Draft<V>> {}

/** Convert a mutable type into a readonly type */
export type Immutable<T> = T extends AtomicObject
export type Immutable<T> = T extends PrimitiveType
? T
: T extends AtomicObject
? T
: T extends Map<infer K, infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableMap<K, V>, "set" | "delete" | "clear">
? ImmutableMap<K, V>
Expand Down
19 changes: 9 additions & 10 deletions src/types/types-external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@ type Tail<T extends any[]> = ((...t: T) => any) extends (
? TT
: []

type PrimitiveType = number | string | boolean

/** Object types that should never be mapped */
type AtomicObject =
| Function
| Promise<any>
| Date
| RegExp
| Boolean
| Number
| String
type AtomicObject = Function | Promise<any> | Date | RegExp

/**
* If the lib "ES2105.collections" is not included in tsconfig.json,
Expand All @@ -42,7 +37,9 @@ type WeakReferences = IfAvailable<WeakMap<any, any>> | IfAvailable<WeakSet<any>>

export type WritableDraft<T> = {-readonly [K in keyof T]: Draft<T[K]>}

export type Draft<T> = T extends AtomicObject
export type Draft<T> = T extends PrimitiveType
? T
: T extends AtomicObject
? T
: T extends IfAvailable<ReadonlyMap<infer K, infer V>> // Map extends ReadonlyMap
? Map<Draft<K>, Draft<V>>
Expand All @@ -55,7 +52,9 @@ export type Draft<T> = T extends AtomicObject
: T

/** Convert a mutable type into a readonly type */
export type Immutable<T> = T extends AtomicObject
export type Immutable<T> = T extends PrimitiveType
? T
: T extends AtomicObject
? T
: T extends IfAvailable<ReadonlyMap<infer K, infer V>> // Map extends ReadonlyMap
? ReadonlyMap<Immutable<K>, Immutable<V>>
Expand Down