MonoTypeOperatorFunction is an RxJS operator function type for operators that preserve the source observable’s value type. Unlike OperatorFunction, it accepts one type parameter because source and result types remain the same. section 5.4.3
Use MonoTypeOperatorFunction when an operator observes, filters, or otherwise processes emissions without changing their type:
- filter restricts emissions without changing their value type. section 5.4.3
- A logging operator can perform a tap side effect while leaving the stream unchanged. section 5.4.3
- The independent pipe function packages reusable operator logic and returns a type-preserving operator. section 5.4.3
Typed custom logging operator
function log<T>(message: string = ''): MonoTypeOperatorFunction<T> { return pipe(tap(item => console.log(`${message ? message + ': ' : ''}${item}`))); } section 5.4.3
Listing 5.12 Using RxJS built-in tools to create a custom operator
function log<T>(
message: string = ''
): MonoTypeOperatorFunction<T> { #1
return pipe( #2
tap(item => console.log(`${message ? message + ': ' : ''}${item}`)),
);
}
Browse the compact implementation showing how MonoTypeOperatorFunction and pipe replace custom operator boilerplate. section 5.4.3
