Suppress an Error in Flow

If you are using Flow to provide static type checking in your JavaScript, you can suppress a flow error message on a one-off basis.

This is convenient, for example, when type checking is working for you 98% of the time but, for a few isolated cases, you just want to turn off type checking (i.e. shut flow up).

Normally, the following example causes a flow type checking error. Can you figure out why?

type Row = {
id:string,
name:string
}
const onChange = (value: Row) => {
const emp: Emp = {
id: value.id,
name: value.name,
idFixed: value.empIdFixed
};
console.log(emp);
};

The reason for the error is that Row does not have an empIdFixed property. However, in this case, the value is coming from an ajax request and I happen to know that it will contain an empIdFixed property at runtime.

The Row type is used in hundreds of places throughout my codebase, so I don’t want to change the Row type. I just want to cheat for this one time.

I first tried to outsmart Flow by using square bracket notation instead of dot notation to access the empIdFixed property:

type Row = {
id:string,
name:string
}
const onChange = (value: Row) => {
const emp: Emp = {
id: value.id,
name: value.name,
idFixed: value["empIdFixed"]
};
console.log(emp);
};

But Flow was too smart for me. It still triggered an error.

So here is what I learned: if you insert the following comment:

//$FlowFixMe

on one line, then the very next line will have errors suppressed:

type Row = {
id:string,
name:string
}
const onChange = (value: Row) => {
const emp: Emp = {
id: value.id,
name: value.name,
//$FlowFixMe
idFixed: value.empIdFixed
};
console.log(emp);
};

Now flow will stop complaining.