Skip to content

Intersection type bug around polymorphic type aliases #342

Closed

Description

@samwgoldman
/* @flow */

type Observable<T> = { observe(observer: Observer<T>): void }
type Observer<T> = { next(x: T): void }
type Subject<T> = Observable<T> & Observer<T>

function createSubject<T>(): Subject<T> {
  var observers = []
  return {
    observe(observer) {
      observers.push(observer)
    },
    next(x) {
      for (var i = 0; i < observers.length; i++) {
        observers[i].next(x)
      }
    }
  }
}

type Test = {
  foo: Observable<string>
}

function test(): Test {
  var foo: Subject<string> = createSubject()
  return { foo }
}
test2.js|5 col 19 error|  intersection type
|| This type is incompatible with
test2.js|3 col 22 error|  object type
|| 
test2.js|5 col 19 error|  intersection type
|| This type is incompatible with
test2.js|4 col 20 error|  object type
|| 

Activity

  1. samwgoldman commented on Mar 26, 2015

    @samwgoldman
    MemberAuthor

    If I define Subject<T> without intersections, but just inlining the definitions from Observable<T> and Observer<T>, then I receive no errors.

  2. samwgoldman commented on Mar 27, 2015

    @samwgoldman
    MemberAuthor

    I was actually able to simplify the test case. The whole Test structure is a red herring.

    /* @flow */
    
    type Observable<T> = { observe(observer: Observer<T>): void }
    type Observer<T> = { next(x: T): void }
    type Subject<T> = Observable<T> & Observer<T>
    
    var observers = []
    var test: Subject<string> = {
      observe(observer) {
        observers.push(observer)
      },
      next(x) {
        for (var i = 0; i < observers.length; i++) {
          observers[i].next(x)
        }
      }
    }
    

    (EDIT: even simpler without the createSubject function.)

  3. samwgoldman commented on Mar 27, 2015

    @samwgoldman
    MemberAuthor

    Is this the same issue as identified in this intersection test?

  4. avikchaudhuri commented on Jun 19, 2015

    @avikchaudhuri
    Contributor

    @samwgoldman This issue has been fixed (both your examples work without errors), hence closing.

    There are other problems remaining with intersection types, but they could be tracked separately. (The test says what you tried should more or less work, but Subject may not be passable to an object type that has both next and observe.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions