如何禁用单行的Flow(JS)类型检查

时间:2019-04-08 22:49:55

标签: javascript unit-testing flowtype

我的一些单元测试涉及将无效(类型不正确的)数据传递给函数。例如:

// 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
    })
})

由于invalidUser变量不符合User类型,因此出现流错误:

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].

显然我希望此变量无效,那么如何为该单个实例禁用流类型检查?

1 个答案:

答案 0 :(得分:3)

根据.flowconfig [options] docs,有一个选项可以指定suppress comment。 Flow将检测到此注释,并忽略以下代码行。

默认情况下:

  

如果您的配置中未指定任何抑制注释,则Flow将应用一个默认值:// $FlowFixMe

因此只需添加注释($FlowFixMe)即可禁止单行的类型检查。

// $FlowFixMe
const invalidUser: User = {}