Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Flow (JS) type checking for a single line

Some of my unit tests involve passing invalid (improperly typed) data to a function. For example:

// user.js

type User = {
    id: number,
    name: string,
    email: string
}

export function validateUser(user: User): Promise<void> {
    return new Promise((resolve, reject) => {
        // resolve if user is valid, reject if not
    })
}
// user.unit.js

import {validateUser} from '../user.js' 

describe('validateUser', () => {
    it('should reject if user is not valid', () => {
        const invalidUser: User = {}
        expect(validateUser(invalidUser)).to.be.rejected
    })
})

Since the invalidUser variable does not conform to the User type, I get a flow error:

Cannot call validateUser with invalidUser bound to user because:
 • property id is missing in object literal [1] but exists in User [2].
 • property name is missing in object literal [1] but exists in User [2].
 • property email is missing in object literal [1] but exists in User [2].

Obviously I want this variable to be invalid, so how can I disable flow type checking for this single instance?

like image 795
Derek Soike Avatar asked Apr 08 '19 22:04

Derek Soike


People also ask

How do you ignore flow error?

$FlowIssue : for a type error that you suspect is an issue with Flow. $FlowExpectedError : for a location where you expect Flow to produce a type error (for instance, when performing an invalid type cast). $FlowIgnore : for locations where you want Flow to ignore your code.


1 Answers

According to the .flowconfig [options] docs, there is an option to specify a suppress comment. Flow will detect this comment and ignore the following line of code.

By default:

If no suppression comments are specified in your config, Flow will apply one default: // $FlowFixMe.

So just add a comment ($FlowFixMe) to suppress type checking for a single line.

// $FlowFixMe
const invalidUser: User = {}
like image 137
Derek Soike Avatar answered Oct 13 '22 02:10

Derek Soike