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

问题描述 投票:2回答:1

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

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

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

javascript unit-testing flowtype
1个回答
3
投票

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

默认情况下:

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

所以只需添加注释($FlowFixMe)来抑制单行的类型检查。

// $FlowFixMe
const invalidUser: User = {}
© www.soinside.com 2019 - 2024. All rights reserved.