简体   繁体   English

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

[英]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: 由于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].

Obviously I want this variable to be invalid, so how can I disable flow type checking for this single instance? 显然,我希望此变量无效,那么如何为该单个实例禁用流类型检查?

According to the .flowconfig [options] docs , there is an option to specify a suppress comment . 根据.flowconfig [options]文档 ,有一个选项可以指定禁止注释 Flow will detect this comment and ignore the following line of code. Flow将检测到此注释,并忽略以下代码行。

By default: 默认:

If no suppression comments are specified in your config, Flow will apply one default: // $FlowFixMe . 如果在配置中未指定任何抑制注释,则Flow将应用一个默认值: // $FlowFixMe

So just add a comment ( $FlowFixMe ) to suppress type checking for a single line. 因此,只需添加注释( $FlowFixMe )即可取消对单行的类型检查。

// $FlowFixMe
const invalidUser: User = {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM