wip w/ aotds

This commit is contained in:
Yanick Champoux 2021-10-14 12:30:06 -04:00
parent 8c026314f5
commit 33ded1448e
5 changed files with 126 additions and 99 deletions

View File

@ -40,7 +40,7 @@
"typescript": "^4.4.3"
},
"license": "MIT",
"main": "dist/index.js",
"main": "src/index.js",
"name": "updux",
"description": "Updeep-friendly Redux helper framework",
"scripts": {

26
types/actions.d.ts vendored Normal file
View File

@ -0,0 +1,26 @@
export type ActionGenerator<TType extends string = string, TPayloadGen = undefined> = {
type: TType;
} & (TPayloadGen extends Function
? (...args: Parameters<TPayloadGen>) => {
type: TType;
payload: ReturnType<TPayloadGen>;
}
: (...args: any[]) => { type: TType; payload?: unknown });
interface Action {
<T extends string = string>(actionType: T): ActionGenerator<T>;
<T extends string = string, F extends Function = Function>(
actionType: T,
payloadGen: F
): ActionGenerator<T, F>;
}
/**
* Creates an action generator.
*/
export const action: Action;
/**
* Utility type for payload generators that are passthroughs.
*/
export type Payload<T = unknown> = (arg: T) => T;

24
types/index.d.ts vendored
View File

@ -5,16 +5,12 @@ type Mutation<TState = unknown> = (
action: Record<string, unknown>
) => (state: TState) => TState;
type ActionGenerator = { type: string } & ((...args: any[]) => {
type: string;
payload?: unknown;
});
export * from './actions';
declare module 'updux' {
/**
/**
* Configuration object typically passed to the constructor of the class Updux.
*/
export interface UpduxConfig<TState = unknown> {
export interface UpduxConfig<TState = unknown> {
/**
* Local initial state.
* @default {}
@ -62,9 +58,9 @@ declare module 'updux' {
* reaction for the mapped dux.
*/
mappedReaction?: Function | boolean;
}
}
export class Updux<TState = unknown> {
export class Updux<TState = unknown> {
constructor(config: Partial<UpduxConfig<TState>>);
get initial(): TState;
@ -99,13 +95,5 @@ declare module 'updux' {
* Registers a selector for the dux.
*/
setSelector(name: string, selector: Selector);
}
/**
* Creates an action generator.
*/
export function action(
actionType: string,
payloadFunction?: Function
): ActionGenerator;
}

View File

@ -1,6 +1,6 @@
import { printType, expectAssignable, expectType } from 'tsd';
import { Updux } from '.';
import { Updux, ActionGenerator, action } from '.';
const dux = new Updux({});
expectType<unknown>( dux.initial );
@ -12,3 +12,16 @@ expectType<unknown>( dux.initial );
expectAssignable<{initial: {a:string}}>(dux);
}
// ActionGenerator
() => {
let a = action('a');
expectAssignable<{type: string}>(a);
expectAssignable<() => {payload?: unknown}>(a);
let b = action('b', (() => ({})) as unknown as (name: string) => { name: string } );
const c = b("foo");
expectAssignable<{payload: { name: string } }>(c);
}

View File