Skip to content

Svelte supports rxjs observables but it is not reflected in types #8726

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

Open
olehmisar opened this issue Jun 13, 2023 · 5 comments
Open

Svelte supports rxjs observables but it is not reflected in types #8726

olehmisar opened this issue Jun 13, 2023 · 5 comments

Comments

@olehmisar
Copy link

olehmisar commented Jun 13, 2023

Describe the bug

I cannot use an rxjs.Observable with derived because svelte's Unsubscriber is not typed properly:

/** Unsubscribes from value updates. */
export type Unsubscriber = () => void;

It should be typed as

export type Unsubscriber = { unsubscribe(): void; } | (() => void);

Reproduction

import { of } from 'rxjs'
const value = of(0);
derived(value, value => value + 2) // type error

Logs

No response

System Info

not relevant

Severity

annoyance

@dummdidumm
Copy link
Member

This is an implementation detail for rxjs interop, but I'm not sure we want to expose it publicly. If you have a rxjs observable, why would you want to use it with derived anyway? Can't use you .pipe and the rxjs operators for that?

@olehmisar
Copy link
Author

I want to mix svelte stores with rx observables

@hackape
Copy link
Contributor

hackape commented Jun 25, 2023

Your request is legit, however the severity is so over-stated. I can easily fix it with @ts-ignore and move on. Please report severity properly.

@samal-rasmussen
Copy link

samal-rasmussen commented Jan 19, 2024

I've taken to using these two helpers in order to get interop:

export function store_to_observable<T>(store: Writable<T> | Readable<T>): Observable<T> {
	return new Observable<T>((observer) => {
		return store.subscribe((value) => observer.next(value));
	});
}

export function observable_to_store<T>(observable: Observable<T>): Readable<T> {
	const store = Readable<T>();
	observable.subscribe((value) => store.set(value));
	return store;
}

But it sound to me like doing this is perfectly safe:

export function observable_to_store<T>(observable: Observable<T>): Readable<T> {
	return observable as any as Readable<T>;
}

Is this correct?

@L2Eme
Copy link

L2Eme commented Apr 10, 2025

observable as unknown as Readable<T>;

It's useful anyway

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

5 participants