Skip to content

StartWith operator should allow widening the type of the Observable #4181

Closed
@rkirov

Description

@rkirov

Bug Report

Current Behavior
The way the types for 'startWith' are written it only allows adding values of the same type as the underlying observable. However, I am observing a lot of usage where people call start with a value outside the original observable type.

AFAICT, there is not fundamental reason why startWith types are so limiting and they can be written to support more permissive usecases.

Reproduction
https://stackblitz.com/edit/typescript-yhhbxn?file=index.ts

const o$ = of(0,1,2);
let o2$ = o$.pipe(
  startWith(null)
)

o2$ is of type Observable<number> and there is no error. With --strictFunctionTypes there is a type error.

Expected behavior
o2$ should be of type Observable<number | null>

Environment

  • RxJS version: 6.3.2

Possible Solution
Change type of startWith to something like:

startWith<S>(x: S) => (Observable<T> => Observable<T | S>)

Additional context/Screenshots
Ngrx users for some reason keeps typing startWith(null) on non-nullable observables.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions