From 0d0d71a453eee78beb3eddf436a6b6409c0df014 Mon Sep 17 00:00:00 2001 From: Kael Watts-Deuchar Date: Wed, 1 May 2019 10:38:18 +1000 Subject: [PATCH 1/2] fix(types): add proper generics to accessors see #38 --- types/index.d.ts | 12 ++++++------ types/test/index.ts | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index e348c4f..f509118 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -52,8 +52,8 @@ export default defaultExport; /*-------------------------------------------------------------------------- SHARED ------------------------------------------------------------------------*/ -type GetAccessor = () => T; -type SetAccessor = (newValue: T) => T; // TODO: Do setters always return same type as input. +type GetAccessor = () => T; +type SetAccessor = (newValue: T) => T; // TODO: Do setters always return same type as input. /*-------------------------------------------------------------------------- make @@ -91,15 +91,15 @@ export class Payload { /*-------------------------------------------------------------------------- get/sync/call ------------------------------------------------------------------------*/ -export function get( +export function get( path: string | object, props?: string[] | object -): { get: GetAccessor }; +): { get: GetAccessor }; -export function sync( +export function sync( path: string | object, props?: string[] | object -): { get: GetAccessor; set: SetAccessor }; +): { get: GetAccessor; set: SetAccessor }; export function call( path: string | object, diff --git a/types/test/index.ts b/types/test/index.ts index 682c3a0..3e2f794 100644 --- a/types/test/index.ts +++ b/types/test/index.ts @@ -1,5 +1,6 @@ +import Vue from 'vue'; import { GetterTree, ActionTree, MutationTree } from "vuex"; -import pathify, { make, Payload } from "../index"; +import pathify, { make, get } from "../index"; interface RootState { name: string; @@ -20,3 +21,17 @@ const actions: ActionTree = { }; const plugin = pathify.plugin; + +Vue.extend({ + computed: { + foo: get('foo'), + bar: get('bar'), + baz: get<{ type: 'baz' }>('baz') + }, + created () { + this.foo // any + this.bar // string + this.baz // object + this.baz.type + } +}) From df3328fad442295353365df7d5ae41ac2c924eaf Mon Sep 17 00:00:00 2001 From: Kael Watts-Deuchar Date: Wed, 1 May 2019 11:07:13 +1000 Subject: [PATCH 2/2] feat(types): add overloads for array and object accessors --- types/index.d.ts | 18 ++++++++++++++++++ types/test/index.ts | 32 +++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index f509118..78192de 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -91,11 +91,29 @@ export class Payload { /*-------------------------------------------------------------------------- get/sync/call ------------------------------------------------------------------------*/ +/** + * Create get accessors + * + * Note type definitions do not support wildcards + */ +export function get( + path: string | object, + props: Array | { [K in keyof T]: string } +): { [K in keyof T]: { get: GetAccessor } }; export function get( path: string | object, props?: string[] | object ): { get: GetAccessor }; +/** + * Create get/set accessors + * + * Note type definitions do not support wildcards + */ +export function sync( + path: string | object, + props: Array | { [K in keyof T]: string } +): { [K in keyof T]: { get: GetAccessor; set: SetAccessor } }; export function sync( path: string | object, props?: string[] | object diff --git a/types/test/index.ts b/types/test/index.ts index 3e2f794..4d82049 100644 --- a/types/test/index.ts +++ b/types/test/index.ts @@ -1,6 +1,6 @@ import Vue from 'vue'; import { GetterTree, ActionTree, MutationTree } from "vuex"; -import pathify, { make, get } from "../index"; +import pathify, { make, get, sync } from "../index"; interface RootState { name: string; @@ -35,3 +35,33 @@ Vue.extend({ 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 + } +})