Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Types for $delegate and $resolve function #1567

Open
wants to merge 6 commits into
base: v4/latest
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Improve typing on $resolve
Added a *breaking change* in Jovo.$resolve typing. I'm try to find a better way without the necessity to change the type of ARGS, but for now it's working for the delegateComponent.
jBernavaPrah committed Jul 8, 2023
commit 42aa899b4baf6b5c86d6885a89c4b83e66ab8200
8 changes: 5 additions & 3 deletions framework/src/BaseDelegateComponent.ts
Original file line number Diff line number Diff line change
@@ -6,8 +6,10 @@ export type ExtractDelegatedEventData<
KEY extends keyof T['__resolve'],
> = T extends BaseDelegateComponent<infer RESOLVE> ? RESOLVE[KEY] : never;

type IsTuple<T> = T extends [any, ...any] ? true : false

export abstract class BaseDelegateComponent<
RESOLVE extends UnknownObject,
RESOLVE extends Record<string, any>,
DATA extends ComponentData = ComponentData,
CONFIG extends UnknownObject = UnknownObject,
> extends BaseComponent<DATA, CONFIG> {
@@ -17,10 +19,10 @@ export abstract class BaseDelegateComponent<

override async $resolve<ARGS extends RESOLVE[KEY], KEY extends keyof RESOLVE = keyof RESOLVE>(
eventName: Extract<KEY, string>,
...eventArgs: ARGS extends Array<unknown> ? [...ARGS] : [ARGS]
...eventArgs: ARGS extends Array<unknown> ? (IsTuple<ARGS> extends true ? ARGS : [ARGS]) : [ARGS]
): Promise<void> {
// because of the JovoProxy class, this implementation of the $resolve will not be called.
// But it's ok, we need only types work.
return super.$resolve(eventName as string, ...(eventArgs as unknown[]));
return super.$resolve(eventName as string, ...eventArgs);
}
}
2 changes: 1 addition & 1 deletion framework/src/Jovo.ts
Original file line number Diff line number Diff line change
@@ -438,7 +438,7 @@ export abstract class Jovo<

// TODO determine whether an error should be thrown if $resolve is called from a context outside a delegation
// TODO Move the implementation to the BaseDelegatedComponent. So also the previously TODO can be removed.
async $resolve<ARGS extends any[]>(eventName: string, ...eventArgs: ARGS): Promise<void> {
async $resolve<ARGS extends [any]>(eventName: string, ...eventArgs: ARGS): Promise<void> {
if (!this.$state) {
return;
}