flow icon indicating copy to clipboard operation
flow copied to clipboard

Can't assign {p:null} to interface with shape {p: ?T}

Open Xesued opened this issue 10 years ago • 3 comments

Here is what I'm trying to do:

/* @flow */
interface Selections {
    selected: any;
    lastSelectedId: ?string;
}

let DEFAULT_SELECTIONS: Selections = {
    selected: {},
    lastSelectedId: null
}

But get an error:

/Users/xesued/testbed/flow/src/index.js:4:19,24: string
This type is incompatible with
/Users/xesued/testbed/flow/src/index.js:9:18,21: null

/Users/xesued/testbed/flow/src/index.js:4:19,24: undefined
This type is incompatible with
/Users/xesued/testbed/flow/src/index.js:9:18,21: null

If I use a type alias it works. If I make it non-nullable and assign the correct type it works as well.

Xesued avatar Mar 16 '16 19:03 Xesued

Thanks! Looks like a real bug. This works for object type aliases, but it seems like it doesn't just for interfaces.

samwgoldman avatar Mar 16 '16 23:03 samwgoldman

So is this definitely a bug or is this a choice made by design? When reading the interface docs, it seems to mention optional properties here, but it doesn't say anything about nullable types. I wasn't sure if this meant that interfaces weren't allowed to have nullable types or if the docs just didn't include info on it.

saadq avatar Jul 17 '17 11:07 saadq

Looks like this works now.

SamChou19815 avatar Mar 13 '22 23:03 SamChou19815