Skip to content
Myr

fix: Add a type-checking fast path for primitive types (#755)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

When `Draft<T>` is applied to a large enum type, TypeScript has to do a lot of unnecessary structural comparisons to confirm that no element of the enum matches `Function`, `Date`, `RegExp`, etc. Determining that they do match `string` or `number`, on the other hand, is trivial. This change splits `PrimitiveType` out of `AtomicObject` so that the fast path can be checked first. In https://github.com/microsoft/TypeScript/issues/42824, this cut the check time from ~2.5 seconds to ~0.3 seconds.

authored by Andrew Casey · committed by GitHub · parent 3b4286d · Browse files · 2 files+17 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+17 added−15 removed

  • other:+17 added−15 removed

2 files+17 added−15 removed

modified:compat/pre-3.7/dist/immer.d.ts+8 added-5 removed
3 unmodified lines
4) => any5 ? TT6 : []78type PrimitiveType = number | string | boolean910/** Object types that should never be mapped */11type AtomicObject =12 | Function13 | WeakMap<any, any>14 | WeakSet<any>15 | Promise<any>16 | Date17 | RegExp16 | Boolean17 | Number18 | String1820export type Draft<T> = T extends AtomicObject19export type Draft<T> = T extends PrimitiveType20 ? T21 : T extends AtomicObject22 ? T23 : T extends Map<infer K, infer V>24 ? DraftMap<K, V>25 : T extends Set<infer V>26 ? DraftSet<V>
7 unmodified lines
34// Inline these in ts 3.735interface DraftSet<V> extends Set<Draft<V>> {}3637/** Convert a mutable type into a readonly type */37export type Immutable<T> = T extends AtomicObject38export type Immutable<T> = T extends PrimitiveType39 ? T40 : T extends AtomicObject41 ? T42 : T extends Map<infer K, infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableMap<K, V>, "set" | "delete" | "clear">43 ? ImmutableMap<K, V>44 : T extends Set<infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableSet<V>, "add" | "delete" | "clear">45 ? ImmutableSet<V>
273 unmodified lines
modified:src/types/types-external.ts+9 added-10 removed