Skip to content

Commit

Permalink
fix(types): workaround for extracting types (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi authored Jan 25, 2022
1 parent 391256e commit 2aa7631
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
28 changes: 22 additions & 6 deletions src/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,28 @@ import {
import { createMutableSource, useMutableSource } from './useMutableSource'
import { getVersion, snapshot, subscribe } from './vanilla'

class SnapshotWrapper<T extends object> {
fn(p: T) {
return snapshot(p)
}
}
type Snapshot<T extends object> = ReturnType<SnapshotWrapper<T>['fn']>
// Unfortunatly, this doesn't work with tsc.
// Hope to find a solution to make this work.
//
// class SnapshotWrapper<T extends object> {
// fn(p: T) {
// return snapshot(p)
// }
// }
// type Snapshot<T extends object> = ReturnType<SnapshotWrapper<T>['fn']>
//
// Using copy-paste types for now:
type AsRef = { $$valtioRef: true }
type AnyFunction = (...args: any[]) => any
type Snapshot<T> = T extends AnyFunction
? T
: T extends AsRef
? T
: T extends Promise<infer V>
? Snapshot<V>
: {
readonly [K in keyof T]: Snapshot<T[K]>
}

const isSSR =
typeof window === 'undefined' ||
Expand Down
28 changes: 22 additions & 6 deletions src/utils/proxyWithComputed.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import { createProxy as createProxyToCompare, isChanged } from 'proxy-compare'
import { proxy, snapshot } from '../vanilla'

class SnapshotWrapper<T extends object> {
fn(p: T) {
return snapshot(p)
}
}
type Snapshot<T extends object> = ReturnType<SnapshotWrapper<T>['fn']>
// Unfortunatly, this doesn't work with tsc.
// Hope to find a solution to make this work.
//
// class SnapshotWrapper<T extends object> {
// fn(p: T) {
// return snapshot(p)
// }
// }
// type Snapshot<T extends object> = ReturnType<SnapshotWrapper<T>['fn']>
//
// Using copy-paste types for now:
type AsRef = { $$valtioRef: true }
type AnyFunction = (...args: any[]) => any
type Snapshot<T> = T extends AnyFunction
? T
: T extends AsRef
? T
: T extends Promise<infer V>
? Snapshot<V>
: {
readonly [K in keyof T]: Snapshot<T[K]>
}

/**
* proxyWithComputed
Expand Down

1 comment on commit 2aa7631

@vercel
Copy link

@vercel vercel bot commented on 2aa7631 Jan 25, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.