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

fix: avoid const enum and deprecate DeepResolveType #333

Merged
merged 1 commit into from
Jan 24, 2022

Conversation

dai-shi
Copy link
Member

@dai-shi dai-shi commented Jan 21, 2022

ref: #327 (comment)
close #292

AsRef const enum was introduced in #146 to fix some typing issue with snapshot. But, this causes some trouble while explicitly typing it, without any workarounds.

This changes AsRef to be hopefully unique type. This allows types to be inferred. While we don't recommend, you can define the same AsRef on your end. It was our strong hope to hide the internal type as implementation detail and it's finally possible.

DeepResolveType was exported previously (which is not considered a public api, but someone might have used it), but it's now deprecated.

Migration Guide

In case you need to specify snapshot types, there are two options.

Assert mutable (let's lie)

type State = { foo?: string }
const state = proxy<State>({})

  const snap = useSnapshot(state) as State
  const handleSnap = (s: State) => {
    // ...
  }

Wrap types with Readonly<>

type State = { foo?: string }
const state = proxy<State>({})

  const snap = useSnapshot(state)
  const handleSnap = (s: Readonly<State>) => {
    // ...
  }

If the state is deeply nested, please define DeepReadonly type util.

type DeepReadonly<T> = {
  readonly [P in keyof T]: DeepReadonly<T[P]>;
}

  const handleSnap = (s: DeepReadonly<State>) => {
    // ...
  }

Other hacks

Extract the return type of snapshot

@vercel
Copy link

vercel bot commented Jan 21, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/pmndrs/valtio/FtwsibPCe39QWo5w5MKvioELkQCM
✅ Preview: https://valtio-git-fix-deprecate-deep-resolve-type-pmndrs.vercel.app

@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 1034f3f:

Sandbox Source
React Configuration
React Typescript Configuration
React Browserify Configuration
React Snowpack Configuration
React Parcel Configuration

@github-actions
Copy link

Size Change: -157 B (0%)

Total Size: 31.7 kB

Filename Size Change
dist/macro.js 910 B -6 B (-1%)
dist/system/macro.js 721 B -12 B (-2%)
dist/system/utils.js 3.44 kB -63 B (-2%)
dist/system/vanilla.js 1.98 kB -43 B (-2%)
dist/umd/macro.js 1.06 kB -11 B (-1%)
dist/umd/utils.js 4 kB -18 B (0%)
dist/umd/vanilla.js 2.1 kB -18 B (-1%)
dist/utils.js 3.84 kB +14 B (0%)
ℹ️ View Unchanged
Filename Size
dist/esm/index.js 1.36 kB
dist/esm/macro.js 613 B
dist/esm/utils.js 3.22 kB
dist/esm/vanilla.js 1.84 kB
dist/index.js 1.45 kB
dist/system/index.js 1.59 kB
dist/umd/index.js 1.59 kB
dist/vanilla.js 1.96 kB

compressed-size-action

@dai-shi dai-shi merged commit d7f4e36 into main Jan 24, 2022
@dai-shi dai-shi deleted the fix/deprecate-deep-resolve-type branch January 24, 2022 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DeepResolveType breaks React.ComponentType
1 participant