Flow: Optional covariant property required since v0.40

Created on 17 Mar 2017  路  2Comments  路  Source: facebook/flow

This used to work in v0.39:

function hasId(obj: { +id?: number }): void {
}

type T = {|
  name: string,
|}

const obj: T = { name: 'ok' }

hasId(obj)

Now breaks since v0.40:
https://flowtype.org/try/#0PQKgBAAgZgNg9gdzCYAoVUCuA7AxgFwEs5swALAQwGcBJAEwAo4AjAKwC4wBvMAakLoB+TtkwBbZgFMATmAC+ASk4A3OAO6o56fAE8ADpLAAVMAF5uAH1Rgw2CmMmcq+aYWwBzADSoLW1LhJnMBYOYzNuW3tHMAByOABrGPl0SlpGEIUgA

Most helpful comment

Well my function takes an object that may have an id, it's not required. Because I'm only reading it, I don't see why flow would not allow me to pass an object that doesn't have one, the function would just read undefined and work as expected. There was no error in v0.39 and I don't see anything in the changelog related to this.

All 2 comments

I'm not sure there's a bug here. Flow clearly know that id will never exist on a T. So I think Flow is working as expected.

Well my function takes an object that may have an id, it's not required. Because I'm only reading it, I don't see why flow would not allow me to pass an object that doesn't have one, the function would just read undefined and work as expected. There was no error in v0.39 and I don't see anything in the changelog related to this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jamiebuilds picture jamiebuilds  路  3Comments

ghost picture ghost  路  3Comments

philikon picture philikon  路  3Comments

damncabbage picture damncabbage  路  3Comments

marcelbeumer picture marcelbeumer  路  3Comments