Skip to content

Commit

Permalink
Further updates to 2-way reactive function set() types
Browse files Browse the repository at this point in the history
- Allow `set()` to return a partial array covering first N inputs to update, and remove extra brackets in `Ins[]` return type since `Ins` is already an array
- Individually validate types of each item of of `{ 0: in0, 2: in2, ... }` object return value, instead of using an index signature that allows values to be any of the inputs' types
  • Loading branch information
ericyhwang authored Sep 12, 2024
1 parent f09052a commit 55bd241
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 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) |
{
get(...inputs: Ins): Out,
set(output: Out, ...inputs: Ins): {[key: number] : Ins[number]} | Ins[] | null,
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 55bd241

Please sign in to comment.