From c02e3092a8d110438539d12a03edcfd61b68b299 Mon Sep 17 00:00:00 2001 From: timpur Date: Mon, 28 May 2018 19:38:35 +1000 Subject: [PATCH 1/3] Inital Complex Typings --- index.d.ts | 46 +++++++++++++++++----------------- preact.d.ts | 71 ++++++++++++++++++++++++++++++++++++++++++----------- react.d.ts | 66 ++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 137 insertions(+), 46 deletions(-) diff --git a/index.d.ts b/index.d.ts index 6e00233..5c716eb 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,29 +1,29 @@ -// T - Wrapped component props -// S - Wrapped component state -// K - Store state -// I - Injected props to wrapped component +declare module "unistore" { + import * as Preact from "preact"; -export type Listener = (state: K, action?: Action) => void; -export type Unsubscribe = () => void; -export type Action = (state: K, ...args: any[]) => void; -export type BoundAction = () => void; + export type Action = (state: TStoreState) => Partial; + export type Action1 = (state: TStoreState, p1: T1) => Partial; + export type Action2 = (state: TStoreState, p1: T1, p2: T2) => Partial; + export type ActionAny = (state: TStoreState, ...args) => Partial; -export interface Store { - action(action: Action): BoundAction; - setState(update: object, overwrite?: boolean, action?: Action): void; - subscribe(f: Listener): Unsubscribe; - unsubscribe(f: Listener): void; - getState(): K; -} + export type BoundAction = () => void; + export type BoundAction1 = (p1: T1) => void; + export type BoundAction2 = (p1: T1, p2: T2) => void; + export type BoundActionAny = (...args) => void; -export default function createStore(state?: K): Store; + export type Listener = (state: TStoreState, action?: ActionAny) => void; + export type Unsubscribe = () => void; -export type ActionFn = (state: K) => object; + export interface Store { + action(action: Action): BoundAction; + action(action: Action1): BoundAction1; + action(action: Action2): BoundAction2; + action(action: ActionAny): BoundActionAny; -export interface ActionMap { - [actionName: string]: ActionFn; + setState(update: object, overwrite?: boolean, action?: Action): void; + subscribe(func: Listener): Unsubscribe; + unsubscribe(func: Listener): void; + getState(): TStoreState; + } + export default function createStore(state?: TStoreState): Store; } - -export type ActionCreator = (store: Store) => ActionMap; - -export type StateMapper = (state: K, props: T) => I; diff --git a/preact.d.ts b/preact.d.ts index 9aa69d2..f116631 100644 --- a/preact.d.ts +++ b/preact.d.ts @@ -1,22 +1,65 @@ -// T - Wrapped component props -// S - Wrapped component state -// K - Store state -// I - Injected props to wrapped component - declare module "unistore/preact" { import * as Preact from "preact"; - import { ActionCreator, StateMapper, Store } from "unistore"; + import { Store, ActionAny } from "unistore"; + + // Diff / Omit taken from https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-311923766 + type Diff = ({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T]; + type Omit = Pick>; + + export interface ProviderProps { + store: Store; + } + export class Provider, TStoreState> extends Preact.Component { + render(props: any, state: any): JSX.Element; + } - export function connect( - mapStateToProps: string | Array | StateMapper, - actions?: ActionCreator | object - ): (Child: (props: T & I) => Preact.VNode) => Preact.ComponentConstructor; + //Props - export interface ProviderProps { - store: Store; + interface MapStateToProps { + (state: TStoreState, ownProps: TExternalProps): TStoreProps; } + type MapStateToPropsParam = MapStateToProps | null | undefined; + + interface MapActionsToPropsFunction { + [actionName: string]: ActionAny | ActionAny; + } + interface MapActionsToPropsFactory { + (store: Store): TActionProps; + } + type MapActionsToPropsParam = MapActionsToPropsFactory; //| MapActionsToPropsFunction; + + type DefaultMapActionToProps = { store: Store } + + interface FunctionalComponent extends Preact.FunctionalComponent { } + interface Component extends Preact.ComponentConstructor { } + type AnyComponent = FunctionalComponent | Component; - export class Provider extends Preact.Component, {}> { - render(props: ProviderProps, {}): Preact.VNode; + interface InferableComponentEnhancerWithProps { + ( + component: AnyComponent + ): Preact.FunctionalComponent & TNeedProps> & { WrappedComponent: Preact.FunctionalComponent } } + + type InferableComponentEnhancer = InferableComponentEnhancerWithProps + + + export interface Connect { + (): InferableComponentEnhancer>; + + ( + mapStateToProps: MapStateToPropsParam + ): InferableComponentEnhancerWithProps, TExternalProps>; + + ( + mapStateToProps: T[] + ): InferableComponentEnhancerWithProps, TExternalProps>; + + ={}, TActionPropsRemap = { [K in keyof TActionProps]: (...args) => void }, TExternalProps={}, TStoreState={}>( + mapStateToProps: MapStateToPropsParam, + mapActionsToProps: MapActionsToPropsParam + ): InferableComponentEnhancerWithProps; + + } + + export const connect: Connect; } diff --git a/react.d.ts b/react.d.ts index 113a729..e7d2324 100644 --- a/react.d.ts +++ b/react.d.ts @@ -5,18 +5,66 @@ declare module "unistore/react" { import * as React from "react"; - import { ActionCreator, StateMapper, Store } from "unistore"; + import { Store, ActionAny } from "unistore"; - export function connect( - mapStateToProps: string | Array | StateMapper, - actions?: ActionCreator | object - ): (Child: (props: T & I) => React.ReactNode) => React.Component; + // Diff / Omit taken from https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-311923766 + type Diff = ({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T]; + type Omit = Pick>; - export interface ProviderProps { - store: Store; + export interface ProviderProps { + store: Store; } - - export class Provider extends Component, {}> { + export class Provider, TStoreState> extends React.Component { render(): React.ReactNode; } + + //Props + + interface MapStateToProps { + (state: TStoreState, ownProps: TExternalProps): TStoreProps; + } + type MapStateToPropsParam = MapStateToProps | null | undefined; + + interface MapActionsToPropsFunction { + [actionName: string]: ActionAny | ActionAny; + } + interface MapActionsToPropsFactory { + (store: Store): TActionProps; + } + type MapActionsToPropsParam = MapActionsToPropsFactory; //| MapActionsToPropsFunction; + + type DefaultMapActionToProps = { store: Store } + + interface FunctionalComponent extends React.StatelessComponent { } + interface Component extends React.Component { } + type AnyComponent = FunctionalComponent | Component; + + interface InferableComponentEnhancerWithProps { + ( + component: AnyComponent + ): FunctionalComponent & TNeedProps> & { WrappedComponent: AnyComponent } + } + + type InferableComponentEnhancer = InferableComponentEnhancerWithProps + + + export interface Connect { + (): InferableComponentEnhancer>; + + ( + mapStateToProps: MapStateToPropsParam + ): InferableComponentEnhancerWithProps, TExternalProps>; + + ( + mapStateToProps: T[] + ): InferableComponentEnhancerWithProps, TExternalProps>; + + ={}, TActionPropsRemap = { [K in keyof TActionProps]: (...args) => void }, TExternalProps={}, TStoreState={}>( + mapStateToProps: MapStateToPropsParam, + mapActionsToProps: MapActionsToPropsParam + ): InferableComponentEnhancerWithProps; + + } + + export const connect: Connect; } From b26a73efd47a4c4e9a22fade45a7859bcbbec44a Mon Sep 17 00:00:00 2001 From: timpur Date: Thu, 7 Jun 2018 19:41:44 +1000 Subject: [PATCH 2/3] Fix up typing issues --- devtools.d.ts | 4 ++++ devtools.js | 2 +- preact.d.ts | 49 +++++++++++++++++++++++++++---------------------- 3 files changed, 32 insertions(+), 23 deletions(-) create mode 100644 devtools.d.ts diff --git a/devtools.d.ts b/devtools.d.ts new file mode 100644 index 0000000..c1ea60b --- /dev/null +++ b/devtools.d.ts @@ -0,0 +1,4 @@ +declare module "unistore/devtools" { + import { Store } from "unistore"; + export default function devtools(store: Store): Store; +} diff --git a/devtools.js b/devtools.js index 6c6b2f1..98ea240 100644 --- a/devtools.js +++ b/devtools.js @@ -1,4 +1,4 @@ -module.exports = function unistoreDevTools(store) { +module.exports.default = function unistoreDevTools(store) { var extension = window.__REDUX_DEVTOOLS_EXTENSION__ || window.top.__REDUX_DEVTOOLS_EXTENSION__; var ignoreState = false; diff --git a/preact.d.ts b/preact.d.ts index f116631..80a0772 100644 --- a/preact.d.ts +++ b/preact.d.ts @@ -1,32 +1,32 @@ declare module "unistore/preact" { import * as Preact from "preact"; - import { Store, ActionAny } from "unistore"; + import { Store, Action, ActionAny } from "unistore"; // Diff / Omit taken from https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-311923766 type Diff = ({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T]; type Omit = Pick>; export interface ProviderProps { - store: Store; + store: Store; } export class Provider, TStoreState> extends Preact.Component { - render(props: any, state: any): JSX.Element; + render(props: any, state: any): JSX.Element; } //Props interface MapStateToProps { - (state: TStoreState, ownProps: TExternalProps): TStoreProps; + (state: TStoreState, ownProps: TExternalProps): TStoreProps; } type MapStateToPropsParam = MapStateToProps | null | undefined; interface MapActionsToPropsFunction { - [actionName: string]: ActionAny | ActionAny; + [actionName: string]: Action | ActionAny; } interface MapActionsToPropsFactory { - (store: Store): TActionProps; + (store: Store): TActionProps; } - type MapActionsToPropsParam = MapActionsToPropsFactory; //| MapActionsToPropsFunction; + type MapActionsToPropsParam = MapActionsToPropsFactory; type DefaultMapActionToProps = { store: Store } @@ -35,31 +35,36 @@ declare module "unistore/preact" { type AnyComponent = FunctionalComponent | Component; interface InferableComponentEnhancerWithProps { - ( - component: AnyComponent - ): Preact.FunctionalComponent & TNeedProps> & { WrappedComponent: Preact.FunctionalComponent } + ( + component: AnyComponent + ): Preact.FunctionalComponent & TNeedProps> & { WrappedComponent: Preact.FunctionalComponent } } type InferableComponentEnhancer = InferableComponentEnhancerWithProps export interface Connect { - (): InferableComponentEnhancer>; + (): InferableComponentEnhancer>; - ( - mapStateToProps: MapStateToPropsParam - ): InferableComponentEnhancerWithProps, TExternalProps>; + ( + mapStateToProps: MapStateToPropsParam + ): InferableComponentEnhancerWithProps, TExternalProps>; - ( - mapStateToProps: T[] - ): InferableComponentEnhancerWithProps, TExternalProps>; + ( + mapStateToProps: T[] + ): InferableComponentEnhancerWithProps, TExternalProps>; - ={}, TActionPropsRemap = { [K in keyof TActionProps]: (...args) => void }, TExternalProps={}, TStoreState={}>( - mapStateToProps: MapStateToPropsParam, - mapActionsToProps: MapActionsToPropsParam - ): InferableComponentEnhancerWithProps; + ={}, TActionPropsRemap = { [K in keyof TActionProps]: (...args) => void }, TExternalProps={}, TStoreState={}>( + mapStateToProps: MapStateToPropsParam, + mapActionsToProps: MapActionsToPropsParam | TActionProps + ): InferableComponentEnhancerWithProps; + + ={}, TActionPropsRemap = { [K in keyof TActionProps]: (...args) => void }, TExternalProps={}, TStoreState={}>( + mapStateToProps: T[], + mapActionsToProps: MapActionsToPropsParam | TActionProps + ): InferableComponentEnhancerWithProps; } export const connect: Connect; -} + } From 51b079c7a857cd7e66e0e7cb824263ddb7ac28d6 Mon Sep 17 00:00:00 2001 From: timpur Date: Mon, 20 Aug 2018 20:40:38 +1000 Subject: [PATCH 3/3] Update types --- index.d.ts | 36 ++++++++++++------------------------ preact.d.ts | 50 +++++++++++++++++++++++++------------------------- react.d.ts | 4 ++-- 3 files changed, 39 insertions(+), 51 deletions(-) diff --git a/index.d.ts b/index.d.ts index 5c716eb..a77340b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,29 +1,17 @@ declare module "unistore" { - import * as Preact from "preact"; + export type Action = (state: TStoreState, ...args: TArgs) => Partial; + export type BoundAction = (...args: TArgs) => void; - export type Action = (state: TStoreState) => Partial; - export type Action1 = (state: TStoreState, p1: T1) => Partial; - export type Action2 = (state: TStoreState, p1: T1, p2: T2) => Partial; - export type ActionAny = (state: TStoreState, ...args) => Partial; + export type Listener = (state: TStoreState, action?: Action) => void; + export type Unsubscribe = () => void; - export type BoundAction = () => void; - export type BoundAction1 = (p1: T1) => void; - export type BoundAction2 = (p1: T1, p2: T2) => void; - export type BoundActionAny = (...args) => void; + export interface Store { + action(action: Action): BoundAction; - export type Listener = (state: TStoreState, action?: ActionAny) => void; - export type Unsubscribe = () => void; - - export interface Store { - action(action: Action): BoundAction; - action(action: Action1): BoundAction1; - action(action: Action2): BoundAction2; - action(action: ActionAny): BoundActionAny; - - setState(update: object, overwrite?: boolean, action?: Action): void; - subscribe(func: Listener): Unsubscribe; - unsubscribe(func: Listener): void; - getState(): TStoreState; - } - export default function createStore(state?: TStoreState): Store; + setState(update: object, overwrite?: boolean, action?: Action): void; + subscribe(func: Listener): Unsubscribe; + unsubscribe(func: Listener): void; + getState(): TStoreState; + } + export default function createStore(state?: TStoreState): Store; } diff --git a/preact.d.ts b/preact.d.ts index 80a0772..9da421b 100644 --- a/preact.d.ts +++ b/preact.d.ts @@ -1,30 +1,30 @@ declare module "unistore/preact" { import * as Preact from "preact"; - import { Store, Action, ActionAny } from "unistore"; + import { Store } from "unistore"; // Diff / Omit taken from https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-311923766 type Diff = ({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T]; type Omit = Pick>; export interface ProviderProps { - store: Store; + store: Store; } export class Provider, TStoreState> extends Preact.Component { - render(props: any, state: any): JSX.Element; + render(props: any, state: any): JSX.Element; } //Props interface MapStateToProps { - (state: TStoreState, ownProps: TExternalProps): TStoreProps; + (state: TStoreState, ownProps: TExternalProps): TStoreProps; } type MapStateToPropsParam = MapStateToProps | null | undefined; interface MapActionsToPropsFunction { - [actionName: string]: Action | ActionAny; + [actionName: string]: (state: TStoreState, ...args: any[]) => Partial; } interface MapActionsToPropsFactory { - (store: Store): TActionProps; + (store: Store): TActionProps; } type MapActionsToPropsParam = MapActionsToPropsFactory; @@ -35,36 +35,36 @@ declare module "unistore/preact" { type AnyComponent = FunctionalComponent | Component; interface InferableComponentEnhancerWithProps { - ( - component: AnyComponent - ): Preact.FunctionalComponent & TNeedProps> & { WrappedComponent: Preact.FunctionalComponent } + ( + component: AnyComponent + ): Preact.FunctionalComponent & TNeedProps> & { WrappedComponent: Preact.FunctionalComponent } } type InferableComponentEnhancer = InferableComponentEnhancerWithProps export interface Connect { - (): InferableComponentEnhancer>; + (): InferableComponentEnhancer>; - ( - mapStateToProps: MapStateToPropsParam - ): InferableComponentEnhancerWithProps, TExternalProps>; + ( + mapStateToProps: MapStateToPropsParam + ): InferableComponentEnhancerWithProps, TExternalProps>; - ( - mapStateToProps: T[] - ): InferableComponentEnhancerWithProps, TExternalProps>; + ( + mapStateToProps: T[] + ): InferableComponentEnhancerWithProps, TExternalProps>; - ={}, TActionPropsRemap = { [K in keyof TActionProps]: (...args) => void }, TExternalProps={}, TStoreState={}>( - mapStateToProps: MapStateToPropsParam, - mapActionsToProps: MapActionsToPropsParam | TActionProps - ): InferableComponentEnhancerWithProps; + ={}, TActionPropsRemap = { [K in keyof TActionProps]: (...args) => void }, TExternalProps={}, TStoreState={}>( + mapStateToProps: MapStateToPropsParam, + mapActionsToProps: MapActionsToPropsParam | TActionProps + ): InferableComponentEnhancerWithProps; - ={}, TActionPropsRemap = { [K in keyof TActionProps]: (...args) => void }, TExternalProps={}, TStoreState={}>( - mapStateToProps: T[], - mapActionsToProps: MapActionsToPropsParam | TActionProps - ): InferableComponentEnhancerWithProps; + ={}, TActionPropsRemap = { [K in keyof TActionProps]: (...args) => void }, TExternalProps={}, TStoreState={}>( + mapStateToProps: T[], + mapActionsToProps: MapActionsToPropsParam | TActionProps + ): InferableComponentEnhancerWithProps; } export const connect: Connect; - } +} diff --git a/react.d.ts b/react.d.ts index e7d2324..65ba881 100644 --- a/react.d.ts +++ b/react.d.ts @@ -5,7 +5,7 @@ declare module "unistore/react" { import * as React from "react"; - import { Store, ActionAny } from "unistore"; + import { Store } from "unistore"; // Diff / Omit taken from https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-311923766 type Diff = ({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T]; @@ -26,7 +26,7 @@ declare module "unistore/react" { type MapStateToPropsParam = MapStateToProps | null | undefined; interface MapActionsToPropsFunction { - [actionName: string]: ActionAny | ActionAny; + [actionName: string]: (state: TStoreState, ...args: any[]) => Partial; } interface MapActionsToPropsFactory { (store: Store): TActionProps;