Flow: Type incompatible error when using optional properties in interface

Created on 20 Feb 2018  路  3Comments  路  Source: facebook/flow

Link to flow try:

/* @flow */
interface X {
  a?: string
};
type Y = {
  a?: string
};

const y : Y = {
  a: 'arst'
};

// Errors out here
(y : X);

Error:

14: (y : X);
     ^ Cannot cast `y` to `X` because undefined [1] is incompatible with string [2] in property `a`.
References:
6:   a?: string         ^ [1]
3:   a?: string         ^ [2]

There are no errors when a is made not optional (link).

It might have something to do with the variance of the property since setting a to write-only works, though read-only doesn't work (which I imagine is more important for most users).

interfaces bug

Most helpful comment

This makes optional properties inside interfaces essentially unusable.

All 3 comments

This makes optional properties inside interfaces essentially unusable.

Bumping this back up, in case it got forgotten.

Was this page helpful?
0 / 5 - 0 ratings