可以使用函数定义来实现这个断言函数,如下所示:
function isNonNullAndOtherProp (arg) {
return arg !== null && arg.someOtherProperty === true
}
其中,arg.someOtherProperty是表示要满足的另一属性。在实际使用中,可以将isNonNullAndOtherProp函数作为参数传递到其他函数中,以确保传入参数的合法性。
示例:
function someFunction (argument, assertionFunc) {
if (!assertionFunc(argument)) {
throw new Error('Invalid argument')
}
// ...
}
const arg = { someOtherProperty: true }
// call someFunction with arg
someFunction(arg, isNonNullAndOtherProp)