staltz / xstream

An extremely intuitive, small, and fast functional reactive stream library for JavaScript
http://staltz.github.io/xstream/
MIT License
2.38k stars 136 forks source link

Types of property '_ils' are incompatible. #316

Open download13 opened 3 years ago

download13 commented 3 years ago

When mergeing streams of component types of a discriminating union the internal listener seems to be trying to assign a variable of the compound type to a variable of a component type.

You can see in the example repository I made that typechecking produces the following error:

index.ts:17:7 - error TS2322: Type 'Stream<FirstComponentType>' is not assignable to type 'Stream<CompoundType>'.
  Types of property '_ils' are incompatible.
    Type 'InternalListener<FirstComponentType>[]' is not assignable to type 'InternalListener<CompoundType>[]'.
      Type 'InternalListener<FirstComponentType>' is not assignable to type 'InternalListener<CompoundType>'.
        Type 'CompoundType' is not assignable to type 'FirstComponentType'.
          Type 'SecondComponentType' is not assignable to type 'FirstComponentType'.

Basically, it seems like merge won't output types compatible with the CompoundType even when all of it's input stream are of component types. It works if merge contains all of the component types however. You can test it by adding second$ to the xs.merge call.

Is this intentional for a reason I don't understand, or is it a bug? I ran into it while converting DOM events into Actions to feed into my reducer function in a cyclejs app.

EDIT: Might be related to https://github.com/staltz/xstream/issues/262#issuecomment-615875854

wclr commented 3 years ago
type Type1 = number
type Type2 = string

type CompoundType = Type1 | Type2;

const first: Type1 = 1;

const first$ = xs.of(first);

const x$: Stream<CompoundType> = first$; // this gives an error

This is due strictFunctionTypes enabled.

But probably it still should be considered as buggy behavior and as can be avoided. I see no reason why Stream<number> should not be actually not assignable to Stream<number | string>.

With xstream it is caused by having visible internal props with InternalListener<T> on the class:

protected _ils: Array<InternalListener<T>>;
protected _dl: InternalListener<T>;

@staltz ?

related issues: https://github.com/staltz/xstream/issues/286

wclr commented 3 years ago

I made a PR for this https://github.com/staltz/xstream/pull/317