Skip to content

Commit

Permalink
fix(types): typescript 5.0 acceptHMRUpdate error (#2098) (#2152)
Browse files Browse the repository at this point in the history
Co-authored-by: kalvens <kalvens@rtvision.com>
  • Loading branch information
posva and kalvenschraut committed Apr 20, 2023
1 parent 1650c6e commit 1469971
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"semver": "^7.5.0",
"typedoc": "^0.23.28",
"typedoc-plugin-markdown": "^3.14.0",
"typescript": "^4.9.4",
"typescript": "^5.0.4",
"vitest": "^0.30.1",
"vue": "^3.2.47",
"yorkie": "^2.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@nuxt/schema": "^3.0.0",
"@nuxt/test-utils": "^3.0.0",
"nuxt": "^3.0.0",
"typescript": "^4.8.4",
"typescript": "^5.0.4",
"vue-tsc": "^1.2.0"
},
"publishConfig": {
Expand Down
17 changes: 15 additions & 2 deletions packages/pinia/src/hmr.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { isRef, isReactive, isVue2, set } from 'vue-demi'
import { Pinia } from './rootStore'
import { isPlainObject, StoreDefinition, StoreGeneric, _Method } from './types'
import {
isPlainObject,
StateTree,
StoreDefinition,
StoreGeneric,
_GettersTree,
_Method,
_ActionsTree,
} from './types'

/**
* Checks if a function is a `StoreDefinition`.
Expand Down Expand Up @@ -70,7 +78,12 @@ export function patchObject(
* @param initialUseStore - return of the defineStore to hot update
* @param hot - `import.meta.hot`
*/
export function acceptHMRUpdate(initialUseStore: StoreDefinition, hot: any) {
export function acceptHMRUpdate<
Id extends string = string,
S extends StateTree = StateTree,
G extends _GettersTree<S> = _GettersTree<S>,
A = _ActionsTree
>(initialUseStore: StoreDefinition<Id, S, G, A>, hot: any) {
// strip as much as possible from iife.prod
if (!__DEV__) {
return () => {}
Expand Down
6 changes: 5 additions & 1 deletion packages/pinia/test-dts/store.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StoreGeneric, defineStore, expectType } from './'
import { StoreGeneric, acceptHMRUpdate, defineStore, expectType } from './'
import { UnwrapRef, watch } from 'vue'

const useStore = defineStore({
Expand Down Expand Up @@ -142,6 +142,10 @@ defineStore({

const store = useStore()

if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useStore, import.meta.hot))
}

expectType<{ a: 'on' | 'off' }>(store.$state)
expectType<number>(store.nested.counter)
expectType<'on' | 'off'>(store.a)
Expand Down
4 changes: 1 addition & 3 deletions packages/playground/src/stores/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { acceptHMRUpdate, defineStore } from 'pinia'

const delay = (t: number) => new Promise((r) => setTimeout(r, t))

export const useCounter = defineStore({
id: 'counter',

export const useCounter = defineStore('counter', {
state: () => ({
n: 2,
incrementedTimes: 0,
Expand Down
68 changes: 40 additions & 28 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1469971

Please sign in to comment.