Skip to content

Commit

Permalink
Improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
zerobias committed Jan 28, 2025
1 parent 6a0702e commit 6c19745
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 42 deletions.
14 changes: 11 additions & 3 deletions packages/core/src/keyval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,20 @@ export function keyval<Input, ModelEnhance, Api, Shape>(
}
| Function,
): Keyval<Input, Input & ModelEnhance, Api, Shape> {
let create;
let create:
| void
| ((config: { onMount: Event<void> }) => {
state: unknown;
api?: unknown;
key: string;
optional?: string[];
});
// @ts-expect-error bad implementation
let getKeyRaw;
let shape, props;
let shape: Shape;
let props;
if (typeof options === 'function') {
create = options;
create = options as any;
} else {
({ key: getKeyRaw, shape = {} as Shape, props, create } = options);
}
Expand Down
43 changes: 7 additions & 36 deletions packages/core/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,12 @@ export function model<
create,
}: {
props: Input;
create: (
props: {
[K in keyof Input]: Input[K] extends
| Store<unknown>
| Event<unknown>
| Effect<unknown, unknown, unknown>
? Input[K]
: Input[K] extends StoreDef<infer V>
? Store<V>
: Input[K] extends EventDef<infer V>
? Event<V>
: Input[K] extends EffectDef<infer V, infer D, infer E>
? Effect<V, D, E>
: Input[K] extends (params: infer P) => infer R
? Effect<P, Awaited<R>>
: Store<Input[K]>;
} & {
[K in {
[P in keyof Input]: Input[P] extends Store<unknown> | StoreDef<unknown>
? P
: never;
}[keyof Input] as K extends string
? `$${K}`
: never]: Input[K] extends Store<unknown>
? Input[K]
: Input[K] extends StoreDef<infer V>
? Store<V>
: never;
},
config: { onMount: Event<void> },
) =>
| { state: Output; api: Api }
| { state?: never; api: Api }
| { state: Output; api?: never }
| Output;
create: (config: { onMount: Event<void> }) => {
state: Output;
api?: Api;
key: string;
optional?: string[];
};
}): Model<
Input,
Show<{
Expand Down Expand Up @@ -108,7 +79,7 @@ export function model<
create,
propsConfig: props,
output: null as unknown as any,
api: null as unknown as any,
// api: null as unknown as any,
shape,
shapeInited: false,
__lens: {} as any,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export function spawn<
});
const parentTracking = childInstancesTracking;
childInstancesTracking = [];
const outputs = model.create(normProps, { onMount: onMount! });
const outputs = model.create({ onMount: onMount! });
const childInstances = childInstancesTracking;
childInstancesTracking = parentTracking;
let storeOutputs = {} as any;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import type { Store, Event, Effect, EventCallable, Node } from 'effector';
export type Model<Props, Output, Api, Shape> = {
type: 'model';
// private
create: (props: any, config: { onMount: Event<void> }) => any;
create: (config: { onMount: Event<void> }) => any;
// private
readonly propsConfig: Props;
readonly output: Output;
// private
readonly __lens: Shape;
// private
readonly api: Api;
// readonly api: Api;
shape: Show<
{
[K in keyof Props]: Props[K] extends Store<infer V>
Expand Down

0 comments on commit 6c19745

Please sign in to comment.