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

Ts 3.4 inference improvements #348

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
3 changes: 1 addition & 2 deletions __tests__/draft.ts
@@ -1,4 +1,4 @@
import {Draft, DraftArray} from "../dist/immer.js"
import {Draft} from "../dist/immer.js"

// For checking if a type is assignable to its draft type (and vice versa)
declare const toDraft: <T>(value: T) => Draft<T>
Expand All @@ -23,7 +23,6 @@ declare const _: any
// NOTE: As of 3.2.2, everything fails without "extends any"
const $ = <Value extends any>(val: ReadonlyArray<Value>) => {
val = _ as Draft<typeof val>
val = _ as DraftArray<typeof val>
let elem: Value = _ as Draft<Value>
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -67,7 +67,7 @@
"regenerator-runtime": "^0.11.1",
"rimraf": "^2.6.2",
"seamless-immutable": "^7.1.3",
"typescript": "3.1.1",
"typescript": "3.4.3",
"yarn-or-npm": "^2.0.4"
},
"jest": {
Expand Down
46 changes: 8 additions & 38 deletions src/immer.d.ts
Expand Up @@ -12,27 +12,17 @@ type AtomicObject =
| Number
| String

type ArrayMethod = Exclude<keyof [], number>
type Indices<T> = Exclude<keyof T, ArrayMethod>

export type DraftArray<T extends ReadonlyArray<any>> = Array<
{[P in Indices<T>]: Draft<T[P]>}[Indices<T>]
>

export type DraftTuple<T extends ReadonlyArray<any>> = {
[P in keyof T]: P extends Indices<T> ? Draft<T[P]> : never
}

export type Draft<T> = T extends never[]
export type Draft<T> = T extends AtomicObject
? T
: T extends ReadonlyArray<any>
? T[number][] extends T
? DraftArray<T>
: DraftTuple<T>
: T extends AtomicObject
: T extends object
? {-readonly [K in keyof T]: Draft<T[K]>}
: T // mostly: unknown & any
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also primitives like number | string | boolean 😛 Or am I misunderstanding this comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eh yes, had a never first there, and AtomicObject at the start, will remove the comment


/** Convert a mutable type into a readonly type */
export type Immutable<T> = T extends AtomicObject
? T
: T extends object
? {-readonly [P in keyof T]: Draft<T[P]>}
? {readonly [K in keyof T]: Immutable<T[K]>}
: T

export interface Patch {
Expand All @@ -52,26 +42,6 @@ export type Produced<Base, Return> = Return extends void
: Return extends Promise<infer Result>
? Promise<Result extends void ? Base : FromNothing<Result>>
: FromNothing<Return>

type ImmutableArray<T extends ReadonlyArray<any>> = {
[P in Extract<keyof T, number>]: ReadonlyArray<Immutable<T[number]>>
}[Extract<keyof T, number>]

type ImmutableTuple<T extends ReadonlyArray<any>> = {
readonly [P in keyof T]: Immutable<T[P]>
}

/** Convert a mutable type into a readonly type */
export type Immutable<T> = T extends object
? T extends AtomicObject
? T
: T extends ReadonlyArray<any>
? Array<T[number]> extends T
? ImmutableArray<T>
: ImmutableTuple<T>
: {readonly [P in keyof T]: Immutable<T[P]>}
: T

export interface IProduce {
/**
* The `produce` function takes a value and a "recipe function" (whose
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Expand Up @@ -6049,10 +6049,10 @@ type-check@~0.3.2:
dependencies:
prelude-ls "~1.1.2"

typescript@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.1.1.tgz#3362ba9dd1e482ebb2355b02dfe8bcd19a2c7c96"
integrity sha512-Veu0w4dTc/9wlWNf2jeRInNodKlcdLgemvPsrNpfu5Pq39sgfFjvIIgTsvUHCoLBnMhPoUA+tFxsXjU6VexVRQ==
typescript@3.4.3:
version "3.4.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.4.3.tgz#0eb320e4ace9b10eadf5bc6103286b0f8b7c224f"
integrity sha512-FFgHdPt4T/duxx6Ndf7hwgMZZjZpB+U0nMNGVCYPq0rEzWKjEDobm4J6yb3CS7naZ0yURFqdw9Gwc7UOh/P9oQ==

uglify-js@^3.1.4:
version "3.5.3"
Expand Down