TypeScript: `startWith`: multiple generics
See original GitHub issueBug 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:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’ve confirmed that this dtslint expectation fails:
And this one passes:
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.
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.