如何禁用单行的 Flow (JS) 类型检查
How to disable Flow (JS) type checking for a single line
我的一些单元测试涉及将无效(输入不正确的)数据传递给函数。例如:
// 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].
显然我希望这个变量无效,那么如何禁用对这个单个实例的流类型检查?
根据.flowconfig [options] docs, there is an option to specify a suppress comment。 Flow 将检测此注释并忽略以下代码行。
默认:
If no suppression comments are specified in your config, Flow will apply one default: // $FlowFixMe
.
所以只需添加注释 ($FlowFixMe
) 以禁止对单行进行类型检查。
// $FlowFixMe
const invalidUser: User = {}
我的一些单元测试涉及将无效(输入不正确的)数据传递给函数。例如:
// 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].
显然我希望这个变量无效,那么如何禁用对这个单个实例的流类型检查?
根据.flowconfig [options] docs, there is an option to specify a suppress comment。 Flow 将检测此注释并忽略以下代码行。
默认:
If no suppression comments are specified in your config, Flow will apply one default:
// $FlowFixMe
.
所以只需添加注释 ($FlowFixMe
) 以禁止对单行进行类型检查。
// $FlowFixMe
const invalidUser: User = {}