admin管理员组文章数量:1323330
I'm using a BroadcastChannel
to pass data from one browser window to another. However, using Flow I get the following error: Flow: property `type` is missing in mixed [1].
This is my code:
const channel = new BroadcastChannel('background');
channel.onmessage = ({ data }) => {
if (data.type === 'OK') {
this.setState({ isLoading: false, success: true, error: false });
}
else if (data.type === 'ERROR') {
this.setState({ isLoading: false, success: false, error: true });
}
};
I also tried to define my own type as such:
type MessageType = {
type: String,
payload: String,
};
...
channel.onmessage = ({ data }: { data: MessageType }) => {
if (data.type === 'OK') {
this.setState({ isLoading: false });
}
if (data.type === 'ERROR') {
alert('ERROR!');
this.setState({ isLoading: false });
}
};
But then Flow gives me the following error: Flow: Cannot assign function to `channel.onmessage` because `MessageType` [1] is inpatible with mixed [2] in property `data` of the first argument.
I figured out that the argument, passed by the message handler is declared like this:
declare class MessageEvent extends Event {
data: mixed;
origin: string;
lastEventId: string;
source: WindowProxy;
}
So, if data is declared to be of type mixed but I need it to be a custom type, how would I do that?
I'm using a BroadcastChannel
to pass data from one browser window to another. However, using Flow I get the following error: Flow: property `type` is missing in mixed [1].
This is my code:
const channel = new BroadcastChannel('background');
channel.onmessage = ({ data }) => {
if (data.type === 'OK') {
this.setState({ isLoading: false, success: true, error: false });
}
else if (data.type === 'ERROR') {
this.setState({ isLoading: false, success: false, error: true });
}
};
I also tried to define my own type as such:
type MessageType = {
type: String,
payload: String,
};
...
channel.onmessage = ({ data }: { data: MessageType }) => {
if (data.type === 'OK') {
this.setState({ isLoading: false });
}
if (data.type === 'ERROR') {
alert('ERROR!');
this.setState({ isLoading: false });
}
};
But then Flow gives me the following error: Flow: Cannot assign function to `channel.onmessage` because `MessageType` [1] is inpatible with mixed [2] in property `data` of the first argument.
I figured out that the argument, passed by the message handler is declared like this:
declare class MessageEvent extends Event {
data: mixed;
origin: string;
lastEventId: string;
source: WindowProxy;
}
So, if data is declared to be of type mixed but I need it to be a custom type, how would I do that?
Share edited Jan 10, 2019 at 16:20 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Jan 9, 2019 at 13:08 lenny.myrlenny.myr 9132 gold badges11 silver badges20 bronze badges1 Answer
Reset to default 7Values of type mixed
can be absolutely anything, including undefined
, null
or objects without a prototype
.
You need to explicitly check that the actual type of data
has a type
field before being able to access it:
channel.onmessage = ({ data }) => {
// 1. Make sure it's not null
// 2. Make sure it's an object. This is only so that we can...
// 3. ...call hasOwnProperty to make sure it has a 'type' field
if(data != null && typeof data === 'object' && data.hasOwnProperty('type')) {
// Inside this condition, Flow knows that the 'type' field exists
if (data.type === 'OK') {
this.setState({ isLoading: false, success: true, error: false });
}
else if (data.type === 'ERROR') {
this.setState({ isLoading: false, success: false, error: true });
}
}
};
本文标签: javascriptFlow property is missing in mixed passedStack Overflow
版权声明:本文标题:javascript - Flow property is missing in mixed passed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742139354a2422512.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论