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(types): add proper generics to accessors #58

Merged
merged 2 commits into from
Jun 22, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export default defaultExport;
/*--------------------------------------------------------------------------
SHARED
------------------------------------------------------------------------*/
type GetAccessor = <T = any>() => T;
type SetAccessor = <T>(newValue: T) => T; // TODO: Do setters always return same type as input.
type GetAccessor<T = any> = () => T;
type SetAccessor<T> = (newValue: T) => T; // TODO: Do setters always return same type as input.

/*--------------------------------------------------------------------------
make
Expand Down Expand Up @@ -91,15 +91,33 @@ export class Payload {
/*--------------------------------------------------------------------------
get/sync/call
------------------------------------------------------------------------*/
export function get(
/**
* Create get accessors
*
* Note type definitions do not support wildcards
*/
export function get<T extends {}>(
path: string | object,
props: Array<keyof T> | { [K in keyof T]: string }
): { [K in keyof T]: { get: GetAccessor<T[K]> } };
export function get<T = any>(
path: string | object,
props?: string[] | object
): { get: GetAccessor };

export function sync(
): { get: GetAccessor<T> };

/**
* Create get/set accessors
*
* Note type definitions do not support wildcards
*/
export function sync<T extends {}>(
path: string | object,
props: Array<keyof T> | { [K in keyof T]: string }
): { [K in keyof T]: { get: GetAccessor<T[K]>; set: SetAccessor<T[K]> } };
export function sync<T = any>(
path: string | object,
props?: string[] | object
): { get: GetAccessor; set: SetAccessor };
): { get: GetAccessor<T>; set: SetAccessor<T> };

export function call(
path: string | object,
Expand Down
47 changes: 46 additions & 1 deletion types/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Vue from 'vue';
import { GetterTree, ActionTree, MutationTree } from "vuex";
import pathify, { make, Payload } from "../index";
import pathify, { make, get, sync } from "../index";

interface RootState {
name: string;
Expand All @@ -20,3 +21,47 @@ const actions: ActionTree<RootState, RootState> = {
};

const plugin = pathify.plugin;

Vue.extend({
computed: {
foo: get('foo'),
bar: get<string>('bar'),
baz: get<{ type: 'baz' }>('baz')
},
created () {
this.foo // any
this.bar // string
this.baz // object
this.baz.type
}
})

Vue.extend({
computed: get<{
search: string,
items: any[]
}>('products', [
'search',
'items',
]),
created () {
this.search
this.items
}
})

Vue.extend({
computed: {
...sync<{
sortOrder: 'asc' | 'desc',
sortKey: string
}>('products/filters@sort', {
sortOrder: 'order',
sortKey: 'key',
})
},
created () {
this.sortOrder
this.sortKey
}
})