Skip to content

Commit 09ec4c7

Browse files
committed
refactor: simplify the inner part of the API that deals with ngOnChanges typings
1 parent bff7248 commit 09ec4c7

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

projects/ngx-observable-lifecycle/src/lib/ngx-observable-lifecycle.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,34 +47,26 @@ export type TypedSimpleChanges<Component, Keys extends keyof Component> = {
4747
};
4848

4949
// none of the hooks have arguments, EXCEPT ngOnChanges which we need to handle differently
50-
export type DecoratedHooks<Component, Keys extends keyof Component> = Record<
50+
export type DecoratedHooks<Component = any, Keys extends keyof Component = any> = Record<
5151
Exclude<LifecycleHookKey, 'ngOnChanges'>,
5252
Observable<void>
5353
> & {
5454
ngOnChanges: Observable<TypedSimpleChanges<Component, Keys>>;
5555
};
56-
export type DecoratedHooksSub<Component, Keys extends keyof Component> = {
57-
[k in keyof DecoratedHooks<Component, Keys>]: DecoratedHooks<Component, Keys>[k] extends Observable<infer U>
58-
? Subject<U>
59-
: never;
56+
export type DecoratedHooksSub = {
57+
[k in keyof DecoratedHooks]: DecoratedHooks[k] extends Observable<infer U> ? Subject<U> : never;
6058
};
6159

62-
type PatchedComponentInstance<Component, Keys extends keyof Component, Hooks extends LifecycleHookKey = any> = Pick<
63-
AllHooks,
64-
Hooks
65-
> & {
66-
[hookSubject]: Pick<DecoratedHooksSub<Component, Keys>, Hooks>;
60+
type PatchedComponentInstance<Hooks extends LifecycleHookKey = any> = Pick<AllHooks, Hooks> & {
61+
[hookSubject]: Pick<DecoratedHooksSub, Hooks>;
6762
constructor: {
6863
prototype: {
6964
[hooksPatched]: Pick<DecorateHookOptions, Hooks>;
7065
};
7166
};
7267
};
7368

74-
function getSubjectForHook<Component, Keys extends keyof Component>(
75-
componentInstance: PatchedComponentInstance<Component, Keys>,
76-
hook: LifecycleHookKey,
77-
): Subject<void> {
69+
function getSubjectForHook(componentInstance: PatchedComponentInstance, hook: LifecycleHookKey): Subject<void> {
7870
if (!componentInstance[hookSubject]) {
7971
componentInstance[hookSubject] = {};
8072
}
@@ -102,7 +94,7 @@ function getSubjectForHook<Component, Keys extends keyof Component>(
10294
};
10395

10496
const originalOnDestroy = proto.ngOnDestroy;
105-
proto.ngOnDestroy = function (this: PatchedComponentInstance<Component, Keys, typeof hook>) {
97+
proto.ngOnDestroy = function (this: PatchedComponentInstance<typeof hook>) {
10698
originalOnDestroy?.call(this);
10799
this[hookSubject]?.[hook]?.complete();
108100
delete this[hookSubject]?.[hook];
@@ -123,7 +115,7 @@ export function getObservableLifecycle<Component, Inputs extends keyof Component
123115
): DecoratedHooks<Component, Inputs> {
124116
return new Proxy({} as DecoratedHooks<Component, Inputs>, {
125117
get(target: DecoratedHooks<Component, Inputs>, p: LifecycleHookKey): Observable<void> {
126-
return getSubjectForHook(classInstance as unknown as PatchedComponentInstance<any, any>, p).asObservable();
118+
return getSubjectForHook(classInstance as unknown as PatchedComponentInstance, p).asObservable();
127119
},
128120
});
129121
}

0 commit comments

Comments
 (0)