refine the actions type

This commit is contained in:
Yanick Champoux 2024-08-12 10:09:16 -04:00
parent 760263b555
commit eec442ff68
3 changed files with 30 additions and 13 deletions

View File

@ -19,6 +19,7 @@ tasks:
- checks - checks
cmds: cmds:
- npm version prerelease - npm version prerelease
- git push --tags
- npm pack --pack-destination releases - npm pack --pack-destination releases
- { task: 'release:gitea' } - { task: 'release:gitea' }

View File

@ -1,4 +1,7 @@
import { ActionCreatorWithPreparedPayload } from '@reduxjs/toolkit'; import {
ActionCreator,
ActionCreatorWithPreparedPayload,
} from '@reduxjs/toolkit';
import { buildActions, createAction, withPayload } from './actions.js'; import { buildActions, createAction, withPayload } from './actions.js';
import Updux from './index.js'; import Updux from './index.js';
@ -42,7 +45,8 @@ test('withPayload, just the type', () => {
test('buildActions', () => { test('buildActions', () => {
const actions = buildActions( const actions = buildActions(
{ {
one: createAction('one'), two: (x: string) => x, one: createAction('one'),
two: (x: string) => x,
withoutValue: null, withoutValue: null,
}, },
{ a: { actions: buildActions({ three: () => 3 }) } }, { a: { actions: buildActions({ three: () => 3 }) } },
@ -87,15 +91,15 @@ describe('Updux interactions', () => {
subduxes: { subduxes: {
subdux1: new Updux({ subdux1: new Updux({
actions: { actions: {
fromSubdux: (x: string) => x fromSubdux: (x: string) => x,
} },
}), }),
subdux2: { subdux2: {
actions: { actions: {
ohmy: () => { } ohmy: () => { },
} },
} },
} },
}); });
test('actions getter', () => { test('actions getter', () => {
@ -103,11 +107,18 @@ describe('Updux interactions', () => {
}); });
expectTypeOf(dux.actions.withNull).toMatchTypeOf< expectTypeOf(dux.actions.withNull).toMatchTypeOf<
ActionCreatorWithPreparedPayload<any, null, 'withNull'>>(); ActionCreator<'withNull'>
>();
expect(dux.actions.withNull()).toMatchObject({
type: 'withNull',
});
expectTypeOf(dux.actions).toMatchTypeOf<Record<string, any>>(); expectTypeOf(dux.actions).toMatchTypeOf<Record<string, any>>();
expectTypeOf(dux.actions?.add).not.toEqualTypeOf<any>(); expectTypeOf(dux.actions?.add).not.toEqualTypeOf<any>();
expect(dux.actions.fromSubdux('payload')).toEqual({ type: 'fromSubdux', payload: 'payload' }); expect(dux.actions.fromSubdux('payload')).toEqual({
type: 'fromSubdux',
payload: 'payload',
});
}); });

View File

@ -45,11 +45,16 @@ type SubduxesActions<D> = D extends {
? UnionToIntersection<DuxActions<UpduxConfig<S[keyof S]>>> ? UnionToIntersection<DuxActions<UpduxConfig<S[keyof S]>>>
: unknown; : unknown;
type ResolveAction<K extends string, A> = A extends Function & { type: string } type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N;
type IsAny<T> = IfAny<T, true, never>;
type ResolveAction<K extends string, A> = true extends IsAny<A>
? ActionCreator<A>
: A extends Function & { type: string }
? A ? A
: A extends (...args: infer PARAMS) => infer R : A extends (...args: infer PARAMS) => infer R
? ActionCreatorWithPreparedPayload<PARAMS, R, K, never, never> ? ActionCreatorWithPreparedPayload<PARAMS, R, K, never, never>
: A; : ActionCreator<A>;
type ResolveActions<A> = A extends { [key: string]: any } type ResolveActions<A> = A extends { [key: string]: any }
? { ? {