欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

​API > @ngrx/effects ofType 有大用


ofType    


'ofType' filters an Observable of Actions into an observable of the actions whose type strings are passed to it.

See more...        

       
ofType<E extends Extract<U, {
    type: T1;
}>, AC extends ActionCreator<string, Creator>, T1 extends string | AC, U extends Action = Action, V = T1>()
       
   

Parameters

There are no parameters.

Description        

For example, if actions has type Actions<AdditionAction|SubstractionAction>, and the type of the Addition action is add, then actions.pipe(ofType('add')) returns an Observable<AdditionAction>.

Properly typing this function is hard and requires some advanced TS tricks below.

Type narrowing automatically works, as long as your actions object starts with a Actions<SomeUnionOfActions> instead of generic Actions.

For backwards compatibility, when one passes a single type argument ofType<T>('something') the result is an Observable<T>. Note, that T completely overrides any possible inference from 'something'.

Unfortunately, for unknown 'actions: Actions' these types will produce 'Observable'. In such cases one has to manually set the generic type like actions.ofType<AdditionAction>('add').

Overloads        

                           
ofType(t1: T1, t2: T2): OperatorFunction<U, V>
                           
                       

Parameters

t1

Type: T1.

t2

Type: T2.

Returns

OperatorFunction<U, V>                        

                           
ofType(t1: T1, t2: T2, t3: T3): OperatorFunction<U, V>
                           
                       

Parameters

t1

Type: T1.

t2

Type: T2.

t3

Type: T3.

Returns

OperatorFunction<U, V>                        

                           
ofType(t1: T1, t2: T2, t3: T3, t4: T4): OperatorFunction<U, V>
                           
                       

Parameters

t1

Type: T1.

t2

Type: T2.

t3

Type: T3.

t4

Type: T4.

Returns

OperatorFunction<U, V>                        

                           
ofType(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5): OperatorFunction<U, V>
                           
                       

Parameters

t1

Type: T1.

t2

Type: T2.

t3

Type: T3.

t4

Type: T4.

t5

Type: T5.

Returns

OperatorFunction<U, V>                        

Fallback for more than 5 arguments. There is no inference, so the return type is the same as the input - Observable.

                           
ofType(...allowedTypes: (string | ActionCreator<string, object>)[]): OperatorFunction<Action, V>
                           
                       

Parameters

allowedTypes

Type: (string | ActionCreator                                            )[],>.

Returns

OperatorFunction<Action, V>                        

We provide a type parameter, even though TS will not infer it from the arguments, to preserve backwards compatibility with old versions of ngrx.

                           
ofType(...allowedTypes: (string | ActionCreator<string, object>)[]): OperatorFunction<Action, Action>
                           
                       

Parameters

allowedTypes

Type: (string | ActionCreator                                            )[],>.

Returns

OperatorFunction<ActionAction>                        

来自  https://ngrx.io/api/effects/ofType

普通分类: