1
1
import { Inject , Injectable } from '@angular/core' ;
2
2
import { Action , ScannedActionsSubject } from '@ngrx/store' ;
3
- import { Observable , Operator , OperatorFunction } from 'rxjs' ;
3
+ import { Observable , OperatorFunction , Operator } from 'rxjs' ;
4
4
import { filter } from 'rxjs/operators' ;
5
5
6
6
@Injectable ( )
@@ -19,20 +19,79 @@ export class Actions<V = Action> extends Observable<V> {
19
19
observable . operator = operator ;
20
20
return observable ;
21
21
}
22
-
23
- /**
24
- * @deprecated from 6.1.0. Use the pipeable `ofType` operator instead.
25
- */
26
- ofType < V2 extends V = V > ( ...allowedTypes : string [ ] ) : Actions < V2 > {
27
- return ofType < any > ( ...allowedTypes ) ( this as Actions < any > ) as Actions < V2 > ;
28
- }
29
22
}
30
23
31
- export function ofType < T extends Action > (
24
+ /**
25
+ * 'ofType' filters an Observable of Actions into an observable of the actions
26
+ * whose type strings are passed to it.
27
+ *
28
+ * For example, `actions.pipe(ofType('add'))` returns an
29
+ * `Observable<AddtionAction>`
30
+ *
31
+ * Properly typing this function is hard and requires some advanced TS tricks
32
+ * below.
33
+ *
34
+ * Type narrowing automatically works, as long as your `actions` object
35
+ * starts with a `Actions<SomeUnionOfActions>` instead of generic `Actions`.
36
+ *
37
+ * For backwards compatibility, when one passes a single type argument
38
+ * `ofType<T>('something')` the result is an `Observable<T>`. Note, that `T`
39
+ * completely overrides any possible inference from 'something'.
40
+ *
41
+ * Unfortunately, for unknown 'actions: Actions' these types will produce
42
+ * 'Observable<never>'. In such cases one has to manually set the generic type
43
+ * like `actions.ofType<AdditionAction>('add')`.
44
+ */
45
+ export function ofType <
46
+ V extends Extract < U , { type : T1 } > ,
47
+ T1 extends string = string ,
48
+ U extends Action = Action
49
+ > ( t1 : T1 ) : OperatorFunction < U , V > ;
50
+ export function ofType <
51
+ V extends Extract < U , { type : T1 | T2 } > ,
52
+ T1 extends string = string ,
53
+ T2 extends string = string ,
54
+ U extends Action = Action
55
+ > ( t1 : T1 , t2 : T2 ) : OperatorFunction < U , V > ;
56
+ export function ofType <
57
+ V extends Extract < U , { type : T1 | T2 | T3 } > ,
58
+ T1 extends string = string ,
59
+ T2 extends string = string ,
60
+ T3 extends string = string ,
61
+ U extends Action = Action
62
+ > ( t1 : T1 , t2 : T2 , t3 : T3 ) : OperatorFunction < U , V > ;
63
+ export function ofType <
64
+ V extends Extract < U , { type : T1 | T2 | T3 | T4 } > ,
65
+ T1 extends string = string ,
66
+ T2 extends string = string ,
67
+ T3 extends string = string ,
68
+ T4 extends string = string ,
69
+ U extends Action = Action
70
+ > ( t1 : T1 , t2 : T2 , t3 : T3 , t4 : T4 ) : OperatorFunction < U , V > ;
71
+ export function ofType <
72
+ V extends Extract < U , { type : T1 | T2 | T3 | T4 | T5 } > ,
73
+ T1 extends string = string ,
74
+ T2 extends string = string ,
75
+ T3 extends string = string ,
76
+ T4 extends string = string ,
77
+ T5 extends string = string ,
78
+ U extends Action = Action
79
+ > ( t1 : T1 , t2 : T2 , t3 : T3 , t4 : T4 , t5 : T5 ) : OperatorFunction < U , V > ;
80
+ /**
81
+ * Fallback for more than 5 arguments.
82
+ * There is no inference, so the return type is the same as the input -
83
+ * Observable<Action>.
84
+ *
85
+ * We provide a type parameter, even though TS will not infer it from the
86
+ * arguments, to preserve backwards compatibility with old versions of ngrx.
87
+ */
88
+ export function ofType < V extends Action > (
89
+ ...allowedTypes : string [ ]
90
+ ) : OperatorFunction < Action , V > ;
91
+ export function ofType (
32
92
...allowedTypes : string [ ]
33
- ) : OperatorFunction < Action , T > {
34
- return filter (
35
- ( action : Action ) : action is T =>
36
- allowedTypes . some ( type => type === action . type )
93
+ ) : OperatorFunction < Action , Action > {
94
+ return filter ( ( action : Action ) =>
95
+ allowedTypes . some ( type => type === action . type )
37
96
) ;
38
97
}
0 commit comments