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

Co/Contravariant stream subtypes #286

Open
DylanRJohnston opened this issue Nov 4, 2019 · 1 comment
Open

Co/Contravariant stream subtypes #286

DylanRJohnston opened this issue Nov 4, 2019 · 1 comment

Comments

@DylanRJohnston
Copy link

DylanRJohnston commented Nov 4, 2019

Hey Staltz, I was wondering if you'd be open to exporting two interface subtypes of Stream<A>, one that contains all of the methods that are contravariant in A maybe called Sink<A> and one that contains all of the methods that are covariant in A maybe called Source<A>. This would allow for proper subtyping of methods that are expecting source / sink streams as Stream<A> is currently invariant in A.

For example

export type ContravariantMethods = 'addListener' | 'removeListener' | 'subscribe'
export interface Sink<A> extends Pick<Stream<A>, ContravariantMethods> {}

Most of the covariant methods on Stream<A> are static, with the exception of remember and debug. So the Source type makes less sense.

@DylanRJohnston
Copy link
Author

DylanRJohnston commented Nov 4, 2019

Example

/* Type 'Stream<3>' is not assignable to type 'Stream<number>'.
  Types of property '_ils' are incompatible.
    Type 'InternalListener<3>[]' is not assignable to type 'InternalListener<number>[]'.
      Type 'InternalListener<3>' is not assignable to type 'InternalListener<number>'.
        Type 'number' is not assignable to type '3'.
*/
const number$: xs.Stream<number> = xs.Stream.of(3 as const)
// Totally fine
const number$: xs.Sink<number> = xs.Stream.of(3 as const)

More concretely, we have a general event sink that expects events payloads of a certain generic structure, and then a system generating events with a more specific type, e.g. a string literal for the event type.

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