This should work
// @flow
'use strict';
type X = ({a:true} & {b:string}) | ({a:false} & {c:string});
// type X = {a:true, b:string} | {a:false, c:string}; // this works.
function hello(x:X): string{
return x.a ? x.b : x.c;
}
This is the error
test.js:8
8: return x.a ? x.b : x.c;
^^^ property `b`. Property cannot be accessed on any member of intersection type
8: return x.a ? x.b : x.c;
^ intersection
Member 1:
4: type X = ({a:true} & {b:string}) | ({a:false} & {c:string});
^^^^^^^^^ object type
Error:
8: return x.a ? x.b : x.c;
^ property `b`. Property not found in
4: type X = ({a:true} & {b:string}) | ({a:false} & {c:string});
^^^^^^^^^ object type
Member 2:
4: type X = ({a:true} & {b:string}) | ({a:false} & {c:string});
^^^^^^^^^^ object type
Error:
8: return x.a ? x.b : x.c;
^ property `b`. Property not found in
4: type X = ({a:true} & {b:string}) | ({a:false} & {c:string});
^^^^^^^^^^ object type
test.js:8
8: return x.a ? x.b : x.c;
^^^ property `c`. Property cannot be accessed on any member of intersection type
8: return x.a ? x.b : x.c;
^ intersection
Member 1:
4: type X = ({a:true} & {b:string}) | ({a:false} & {c:string});
^^^^^^^^ object type
Error:
8: return x.a ? x.b : x.c;
^ property `c`. Property not found in
4: type X = ({a:true} & {b:string}) | ({a:false} & {c:string});
^^^^^^^^ object type
Member 2:
4: type X = ({a:true} & {b:string}) | ({a:false} & {c:string});
^^^^^^^^^^ object type
Error:
8: return x.a ? x.b : x.c;
^ property `c`. Property not found in
4: type X = ({a:true} & {b:string}) | ({a:false} & {c:string});
^^^^^^^^^^ object type
I think &
doesn't quite work correctly for extending objects.
what other alternatives can i use to extend an object?
still this is a bug. i can use &
without any problem as long as i don't combine it with |
I can't always use &
on it's own either. It works most of the times, but every now and then it just breaks down.
It tends to not work when using flow check-contents
in a linter plugin
I'm trying to find workaround for now.
This is going to be fixed in an upcoming release.
+1
@avikchaudhuri I'm still seeing this error, but I'm probably approaching the issue incorrectly. https://github.com/facebook/flow/issues/2135
I am still seeing this issue.
// ... generated.js
// Transaction represents a single atomic transaction in the application
export type Transaction = {
description: string,
bidder_id: number, // foreign key to bidder table
stripe_transaction_id: string,
event_id: number, // foreign key to event table
amount: number, // in cents
fee: number, // in cents
status: string
}
import type {
Transaction as transaction,
} from './generated';
// merge types with this ID until Typewriter can get this from the embedded types
type ID = { ID: number };
type UpdatedAt = { UpdatedAt: string };
export type Transaction = ID & UpdatedAt & transaction;
results in: property `ID`
Property cannot be accessed on
mixed
+2
Why is this closed? I still have the same issue I posted above with .47
Still get this error. Just created a new issue.
Most helpful comment
Why is this closed? I still have the same issue I posted above with .47