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

Intersection type bug around polymorphic type aliases #342

Closed
samwgoldman opened this issue Mar 26, 2015 · 4 comments
Closed

Intersection type bug around polymorphic type aliases #342

samwgoldman opened this issue Mar 26, 2015 · 4 comments

Comments

@samwgoldman
Copy link
Member

/* @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
|| 
@samwgoldman
Copy link
Member Author

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

@samwgoldman
Copy link
Member Author

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.)

@samwgoldman
Copy link
Member Author

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

@avikchaudhuri
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants