Skip to content

Commit c539b78

Browse files
alex-okrushkobrandonroberts
authored andcommitted
refactor(effects): prepare for TS 3.5 (#2191)
1 parent ccd3dd7 commit c539b78

File tree

5 files changed

+37
-25
lines changed

5 files changed

+37
-25
lines changed

modules/effects/src/effect_creator.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ export function createEffect<
6161
return effect;
6262
}
6363

64-
export function getCreateEffectMetadata<T>(instance: T): EffectMetadata<T>[] {
64+
export function getCreateEffectMetadata<
65+
T extends { [props in keyof T]: Object }
66+
>(instance: T): EffectMetadata<T>[] {
6567
const propertyNames = Object.getOwnPropertyNames(instance) as Extract<
6668
keyof T,
6769
string

modules/effects/src/effect_decorator.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { compose } from '@ngrx/store';
2-
import { EffectMetadata, EffectConfig } from './models';
2+
3+
import { EffectConfig, EffectMetadata, EffectPropertyKey } from './models';
34
import { getSourceForInstance } from './utils';
45

56
const METADATA_KEY = '__@ngrx/effects__';
67

7-
export function Effect<T>({
8+
export function Effect({
89
dispatch = true,
910
resubscribeOnError = true,
10-
}: EffectConfig = {}): PropertyDecorator {
11-
return function<K extends Extract<keyof T, string>>(
11+
}: EffectConfig = {}) {
12+
return function<T extends Object, K extends EffectPropertyKey<T>>(
1213
target: T,
1314
propertyName: K
1415
) {
@@ -21,7 +22,7 @@ export function Effect<T>({
2122
resubscribeOnError,
2223
};
2324
setEffectMetadataEntries<T>(target, [metadata]);
24-
} as (target: {}, propertyName: string | symbol) => void;
25+
};
2526
}
2627

2728
export function getEffectDecoratorMetadata<T>(
@@ -35,7 +36,7 @@ export function getEffectDecoratorMetadata<T>(
3536
return effectsDecorators;
3637
}
3738

38-
function setEffectMetadataEntries<T>(
39+
function setEffectMetadataEntries<T extends object>(
3940
sourceProto: T,
4041
entries: EffectMetadata<T>[]
4142
) {
@@ -50,8 +51,12 @@ function setEffectMetadataEntries<T>(
5051
Array.prototype.push.apply(meta, entries);
5152
}
5253

53-
function getEffectMetadataEntries<T>(sourceProto: T): EffectMetadata<T>[] {
54+
function getEffectMetadataEntries<T extends object>(
55+
sourceProto: T
56+
): EffectMetadata<T>[] {
5457
return sourceProto.constructor.hasOwnProperty(METADATA_KEY)
55-
? (sourceProto.constructor as any)[METADATA_KEY]
58+
? (sourceProto.constructor as typeof sourceProto.constructor & {
59+
[METADATA_KEY]: EffectMetadata<T>[];
60+
})[METADATA_KEY]
5661
: [];
5762
}

modules/effects/src/effect_notification.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Notification, Observable } from 'rxjs';
44

55
export interface EffectNotification {
66
effect: Observable<any> | (() => Observable<any>);
7-
propertyName: string;
7+
propertyName: PropertyKey;
88
sourceName: string;
99
sourceInstance: any;
1010
notification: Notification<Action | null | undefined>;
@@ -46,7 +46,7 @@ function getEffectName({
4646
}: EffectNotification) {
4747
const isMethod = typeof sourceInstance[propertyName] === 'function';
4848

49-
return `"${sourceName}.${propertyName}${isMethod ? '()' : ''}"`;
49+
return `"${sourceName}.${String(propertyName)}${isMethod ? '()' : ''}"`;
5050
}
5151

5252
function stringify(action: Action | null | undefined) {

modules/effects/src/effects_metadata.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ import { getCreateEffectMetadata } from './effect_creator';
33
import { getEffectDecoratorMetadata } from './effect_decorator';
44

55
export function getEffectsMetadata<T>(instance: T): EffectsMetadata<T> {
6-
const metadata: EffectsMetadata<T> = {};
7-
8-
for (const {
9-
propertyName,
10-
dispatch,
11-
resubscribeOnError,
12-
} of getSourceMetadata(instance)) {
13-
metadata[propertyName] = { dispatch, resubscribeOnError };
14-
}
15-
16-
return metadata;
6+
return getSourceMetadata(instance).reduce(
7+
(
8+
acc: EffectsMetadata<T>,
9+
{ propertyName, dispatch, resubscribeOnError }
10+
) => {
11+
acc[propertyName] = { dispatch, resubscribeOnError };
12+
return acc;
13+
},
14+
{}
15+
);
1716
}
1817

1918
export function getSourceMetadata<T>(instance: T): EffectMetadata<T>[] {

modules/effects/src/models.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ export interface EffectConfig {
1313
resubscribeOnError?: boolean;
1414
}
1515

16-
export interface EffectMetadata<T> extends Required<EffectConfig> {
17-
propertyName: Extract<keyof T, string>;
16+
export type EffectPropertyKey<T extends Object> = Exclude<
17+
keyof T,
18+
keyof Object
19+
>;
20+
21+
export interface EffectMetadata<T extends Object>
22+
extends Required<EffectConfig> {
23+
propertyName: EffectPropertyKey<T>;
1824
}
1925

2026
export type EffectsMetadata<T> = {
21-
[key in Extract<keyof T, string>]?: EffectConfig
27+
[key in EffectPropertyKey<T>]?: EffectConfig
2228
};

0 commit comments

Comments
 (0)