Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defaultIfEmpty does not give correct type for null or undefined when strictNullChecks is false in tsconfig #7280

Open
shessafridi opened this issue Jun 4, 2023 · 0 comments

Comments

@shessafridi
Copy link

Describe the bug

defaultIfEmpty(null) or defaultIfEmpty(undefined) always returns the observable of type Observable<any>

Expected behavior

When strictNullChecks is off null type is ignored so it should just return the type of the source observable as it is.

Expected behaviour in strictNullChecks: false

const test$ = of(1).pipe(defaultIfEmpty(null)); // Observable<number>

Instead we get

const test$ = of(1).pipe(defaultIfEmpty(null)); // Observable<any>

Reproduction code

const test = of(1).pipe(defaultIfEmpty(null));

Reproduction URL

No response

Version

7.8.1

Environment

"typescript": "~5.0.4",
"rxjs": "^7.8.1"

Additional context

I am currently using a custom wrapper operator built over defaultIfEmpty to fix the issue, here is the code for it.

import { defaultIfEmpty, Observable } from 'rxjs';

export function defaultIfEmptyFixed<T, R extends null>(
  defaultValue: R
): (source: Observable<T>) => Observable<T | null>;

export function defaultIfEmptyFixed<T, R extends undefined>(
  defaultValue: R
): (source: Observable<T>) => Observable<T | undefined>;

export function defaultIfEmptyFixed<T, R>(
  defaultValue: R
): (source: Observable<T>) => Observable<T | R> {
  return (input: Observable<T>) =>
    input.pipe(defaultIfEmpty<T, R>(defaultValue));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant