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

feat(core/d.ts): support createStoreWithThis's type inference #631

Merged
merged 14 commits into from Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all 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: 2 additions & 1 deletion package.json
Expand Up @@ -32,6 +32,7 @@
"jest": "^24.8.0",
"lerna": "^3.4.3",
"vue": "^2.6.11",
"vuepress": "^1.4.0"
"vuepress": "^1.4.0",
"typescript": "^4.1.0-beta"
}
}
57 changes: 49 additions & 8 deletions packages/core/@types/index.d.ts
@@ -1,7 +1,7 @@
// Type definitions for @mpxjs/core
// Project: https://github.com/didi/mpx
// Definitions by: hiyuki <https://github.com/hiyuki>
// TypeScript Version: 4.0.2
// TypeScript Version: 4.1.0-beta

/// <reference types="miniprogram-api-typings" />

Expand Down Expand Up @@ -87,6 +87,10 @@ type UnionToIntersection<U> = (U extends any
? I
: never;

type RemoveNeverProps<T> = Pick<T, {
[K in keyof T]: T[K] extends never ? never : K
}[keyof T]>

type ArrayType<T extends any[]> = T extends Array<infer R> ? R : never;

type RequiredPropertyNames<T> = {
Expand Down Expand Up @@ -317,18 +321,41 @@ interface MutationsAndActionsWithThis {
[key: string]: (...payload: any[]) => any
}

type GetDispatchAndCommitWithThis<A, D> = keyof D extends never ? (<T extends keyof A>(type: T, ...payload: A[T] extends (...payload: infer P) => any ? P : never) => A[T] extends (...payload: any[]) => infer R ? R : never) : ((type: string, ...payload: any[]) => any)
interface DeeperMutationsAndActions {
[key: string]: ((...payload: any[]) => any)|MutationsAndActionsWithThis
}

// Store Type Bindings
type StringKeyof<T> = Exclude<keyof T, symbol>

type CombineStringKey<H extends string | number, L extends string | number> = H extends '' ? `${L}` : `${H}.${L}`

type GetActionsKey<A, P extends string|number = ''> = UnionToIntersection<{
[K in StringKeyof<A>]: {
[RK in CombineStringKey<P, K>]: A[K] extends DeeperMutationsAndActions ? GetActionsKey<A[K], RK> : Record<RK, A[K]>
}[CombineStringKey<P, K>]
}[StringKeyof<A>]> // {actA: () => void, storeB.actB: () => void}

type GetAllActionsKey<A, D extends Deps, AK extends 'actions'|'mutations'> = {
[K in StringKeyof<A>]: A[K]
} & UnionToIntersection<{
[K in StringKeyof<D>]: {
[P in keyof GetActionsKey<D[K][AK], K>]: GetActionsKey<D[K][AK], K>[P]
}
}[StringKeyof<D>]>


type GetDispatchAndCommitWithThis<A, D extends Deps, AK extends 'actions'|'mutations'> = (<T extends keyof GetAllActionsKey<A, D, AK>>(type: T, ...payload: GetAllActionsKey<A, D, AK>[T] extends (...payload: infer P) => any ? P : never) => GetAllActionsKey<A, D, AK>[T] extends (...payload: any[]) => infer R ? R : never)
interface StoreOptWithThis<S, G, M, A, D extends Deps> {
state?: S
getters?: G & ThisType<{ state: S & UnboxDepsField<D, 'state'>, getters: G & UnboxDepsField<D, 'getters'>, rootState: any }>
getters?: G & ThisType<{ state: S & UnboxDepsField<D, 'state'>, getters: GetComputedType<G> & UnboxDepsField<D, 'getters'>, rootState: any }>
mutations?: M & ThisType<{ state: S & UnboxDepsField<D, 'state'> }>
actions?: A & ThisType<{
rootState: any,
state: S & UnboxDepsField<D, 'state'>,
getters: GetComputedType<G> & UnboxDepsField<D, 'getters'>,
dispatch: GetDispatchAndCommitWithThis<A, D>,
commit: GetDispatchAndCommitWithThis<M, D>
dispatch: GetDispatchAndCommitWithThis<A, D, 'actions'>,
commit: GetDispatchAndCommitWithThis<M, D, 'mutations'>
}>
deps?: D
modules?: ObjectOf<StoreOptWithThis<{}, {}, {}, {}, {}>>
Expand All @@ -345,9 +372,9 @@ declare class StoreWithThis<S = {}, G = {}, M = {}, A = {}, D extends Deps = {}>
mutations: M & UnboxDepsField<D, 'mutations'>
actions: A & UnboxDepsField<D, 'actions'>

dispatch: GetDispatchAndCommitWithThis<A, D>
dispatch: GetDispatchAndCommitWithThis<A, D, 'actions'>

commit: GetDispatchAndCommitWithThis<M, D>
commit: GetDispatchAndCommitWithThis<M, D, 'mutations'>

mapState<K extends keyof S> (maps: K[]): {
[I in K]: () => S[I]
Expand Down Expand Up @@ -403,9 +430,23 @@ declare class StoreWithThis<S = {}, G = {}, M = {}, A = {}, D extends Deps = {}>

export function createStoreWithThis<S = {}, G = {}, M extends MutationsAndActionsWithThis = {}, A extends MutationsAndActionsWithThis = {}, D extends Deps = {}> (option: StoreOptWithThis<S, G, M, A, D>): StoreWithThis<S, G, M, A, D>

// auxiliary functions
export function createStateWithThis<S = {}> (state: S): S

export function injectMixins (mixins: object | Array<object>, type?: 'app' | 'page' | 'component'): void
export function createGettersWithThis<S = {}, D extends Deps = {}, G = {}> (state: S, getters: G & ThisType<{ state: S & UnboxDepsField<D, 'state'>, getters: GetComputedType<G> & UnboxDepsField<D, 'getters'>, rootState: any }>, deps?: D): G

export function createMutationsWithThis<S = {}, D extends Deps = {}, M extends MutationsAndActionsWithThis = {}> (state: S, mutations: M & ThisType<{ state: S & UnboxDepsField<D, 'state'> }>, deps?: D): M

export function createActionsWithThis<S = {}, G = {}, M extends MutationsAndActionsWithThis = {}, D extends Deps = {}, A extends MutationsAndActionsWithThis = {}> (state: S, getters: G, mutations: M & ThisType<{ state: S & UnboxDepsField<D, 'state'> }>, actions: A & ThisType<{
rootState: any,
state: S & UnboxDepsField<D, 'state'>,
getters: GetComputedType<G> & UnboxDepsField<D, 'getters'>,
dispatch: GetDispatchAndCommitWithThis<A, D, 'actions'>,
commit: GetDispatchAndCommitWithThis<M, D, 'mutations'>
}>, deps?: D): A


export function injectMixins (mixins: object | Array<object>, type?: 'app' | 'page' | 'component'): void

declare class Watcher {
constructor (context: any, expr: string | (() => any), handler: WatchHandler | WatchOptWithHandler, options?: WatchOpt)
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/core/createStore.js
Expand Up @@ -218,6 +218,20 @@ export default function createStore (options) {
return new Store(options)
}

// ts util functions
export function createStateWithThis (state) {
return state
}
export function createGettersWithThis (state, getters, deps = {}) {
return getters
}
export function createMutationsWithThis (state, mutations, deps = {}) {
return mutations
}
export function createActionsWithThis (state, getters, mutations, actions, deps = {}) {
return actions
}

export function createStoreWithThis (options) {
options.withThis = true
return new Store(options)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/index.js
@@ -1,5 +1,5 @@
import * as platform from './platform'
import createStore, { createStoreWithThis } from './core/createStore'
import createStore, { createStoreWithThis, createStateWithThis, createGettersWithThis, createMutationsWithThis, createActionsWithThis } from './core/createStore'
import { injectMixins } from './core/injectMixins'
import { extend, diffAndCloneA, makeMap } from './helper/utils'
import { setConvertRule } from './convertor/convertor'
Expand All @@ -25,7 +25,7 @@ export function createComponent (config, ...rest) {
platform.createComponent(Object.assign({ proto: mpx.proto }, config), ...rest)
}

export { createStore, createStoreWithThis, getMixin }
export { createStore, createStoreWithThis, createStateWithThis, createGettersWithThis, createMutationsWithThis, createActionsWithThis, getMixin }

export function toPureObject (obj) {
return diffAndCloneA(obj).clone
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-plugin/lib/template-compiler/compiler.js
Expand Up @@ -915,7 +915,7 @@ function parse (template, options) {
type: 3,
text: text,
parent: currentParent,
isComment: true,
isComment: true
})
}
}
Expand Down