question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript: `startWith`: multiple generics

See original GitHub issue

Bug Report

Reproduction

declare const foo$: Observable<{ foo: number }>
// Expected type: { foo: number }
// Actual type: { foo: number } | { foo: number }
const bar$ = foo$.pipe(startWith({ foo: 1 }))

Although the expected/actual types are compatible, it’s slightly irritating to deal with a union type T | T instead of a consolidated type T, as it makes type inspection in VS Code much more difficult (especially when the types are large or deeply nested).

Is there a way to tell startWith to consolidate its generics if they are the same? I tried adding an overload with just one generic, but then other tests started to fail.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cartantcommented, Jun 17, 2019

I’ve confirmed that this dtslint expectation fails:

const o = of<{ foo: number }>().pipe(startWith({ foo: 1 })); // $ExpectType Observable<{ foo: number; }>
ERROR: 5:3  expect  Expected type to be:
  Observable<{ foo: number; }>
got:
  Observable<{ foo: number; } | { foo: number; }>

And this one passes:

const v = { foo: 1 };
const o = of<typeof v>().pipe(startWith(v)); // $ExpectType Observable<{ foo: number; }>

However, I’d suggest that this is a TypeScript issue and for that reason I’d be reluctant to implement - what would be, IMO - a workaround.

I cannot see the reason for TypeScript’s not consolidating a union of identical shapes.

0reactions
cartantcommented, May 27, 2021

This remains an issue with RxJS 7.1 and TypeScript 4.3, but, IMO, this could still be closed as TypeScript quirk. I note that the related TypeScript issue is still open.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Use Generics in TypeScript - DigitalOcean
TypeScript fully supports generics as a way to introduce type-safety into components that accept arguments and return values whose type will be ...
Read more >
How to limit a typescript generic to one of two primitives?
I would like a way to use the typescript generics, and the constraints to get the following to compile. function GetValue<T extends number...
Read more >
Playground Example - Generic Functions - TypeScript
Let's start with an example where we wrap an input object in an array. ... Generics can start to look complicated when you...
Read more >
How To Use Generics in Typescript - Reusable Parts of Code
Thanks to Generic Types you can easily create reusable functions, interfaces or classes instead of writing single types for each one.
Read more >
TypeScript Generics – Use Case and Examples
To use generics, you need to use angular brackets and then specify a type variable inside them. Developers generally use T , X...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found