Skip to content

Commit

Permalink
Merge pull request #314 from sophiasam96/update-modelfn-type
Browse files Browse the repository at this point in the history
Update `model.start()` type for two-way reactive functions
  • Loading branch information
ericyhwang authored Sep 13, 2024
2 parents e723582 + 55bd241 commit 9a373dd
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/Model/fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ class NamedFns { }

type StartFnParam = unknown;

// From type-fest: https://github.com/sindresorhus/type-fest
type ArrayIndices<Element extends readonly unknown[]> =
Exclude<Partial<Element>['length'], Element['length']>;

type TwoWayReactiveFnSetReturnType<Ins extends readonly unknown[]> =
Partial<Ins> |
Partial<{ [K in Extract<ArrayIndices<Ins>, number>]: Ins[K] }> |
null;

type ModelFn<Ins extends unknown[], Out> =
(...inputs: Ins) => Out |
((...inputs: Ins) => Out) |
{
get(...inputs: Ins): Out,
set(output: Out, ...inputs: Ins): void,
get(...inputs: Ins): Out;
set(output: Out, ...inputs: Ins): TwoWayReactiveFnSetReturnType<Ins>;
};

interface ModelStartOptions {
Expand Down Expand Up @@ -99,11 +108,7 @@ declare module './Model' {
*/
fn<Ins extends unknown[], Out>(
name: string,
fn: (...inputs: Ins) => Out |
{
get(...inputs: Ins): Out;
set(output: Out, ...inputs: Ins): void
}
fn: ModelFn<Ins, Out>
): void;

/**
Expand Down

0 comments on commit 9a373dd

Please sign in to comment.