-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Object literal {} incompatible with $Exact<{}> #4582
Comments
Or to simplify without an alias... const foo: {| bar: 'bar' |} = { bar: 'bar' }
const fooError: {||} = {} 4: const fooError: {||} = {}
^ object literal. Inexact type is incompatible with exact type
4: const fooError: {||} = {}
^ exact type: object type You're right, this is a surprising behavior. |
+1 |
Sometimes `{ [any]: empty }` is used as a workaround instead of `{||}`. See: - facebook/flow#2977 - facebook/flow#4582
Sometimes `{ [any]: empty }` is used as a workaround instead of `{||}`. See: - facebook/flow#2977 - facebook/flow#4582
I encountered this when trying to assign an empty object to an exact type defined with an indexer. type Exact = {|
[KeyType]: ValueType
|};
type PossiblyEmptyExact = Exact | {}; |
That's not a better workaround since
is happily accepted by the compiler. Using a non-exact type would be more type-safe. |
@marcianx: Thanks. Seems like I was so happy it worked, I didn't think it through. |
Guys, please beware of this issue with shapes: #5884. The currently accept |
@marcianx: type T = $Shape<{ a: number, b: number }>;
const partial: T = { a: 1 }; // no errors In my case |
@mhagmajer Thanks for pointing this out! The easy workaround for this is to ensure that this is an object via
@hon2a Thanks for mentioning. I should have explicitly mentioned this -- indeed, this workaround is only for the case where you wish all fields to be optional (which was my case when I ran into this bug). |
I think this code does not work for the same reason. type T = {|a?: number|};
const t: T = {}; |
Closing in favour of #2977 |
The only work around seems to be to replace
$Exact<{}>
with{ [any]: empty }
The text was updated successfully, but these errors were encountered: