我写了一个jest.Matchers的简单扩展,但我无法通过打字稿类型检查器识别我的扩展名。
我使用的是纯JavaScript。
// @ts-check
const getFunctorValue = F => {
let x
F.fmap(v => x = v)
return x
}
expect.extend({
/**
* @extends jest.Matchers
* @param {*} actual The functor you want to test.
* @param {*} expected The functor you expect.
*/
functorToBe(actual, expected) {
const actualValue = getFunctorValue(actual)
const expectedValue = getFunctorValue(expected)
const pass = Object.is(actualValue, expectedValue)
return {
pass,
message () {
return `expected ${actualValue} of ${actual} to ${pass ? '' : 'not'} be ${expectedValue} of ${expected}`
}
}
}
})
/**
* @constructor
* @param {*} v Any value
*/
function just (v) {
return {
fmap: f => just(f(v))
}
}
describe('Functor Law', () => {
test('equational reasoning (identity)', () => {
expect(just(1)).functorToBe(just(1))
})
})
但是与expect(just(1)).functorToBe(just(1))
一致,我在functorToBe
下面有一个红色下划线,并出现以下错误信息:
[ts]属性'functorToBe'在类型'Matchers <{[x:string]:any;上不存在fmap:(f:any)=> any; }>”。任何
我从jest.Matchers
写expect()
得到了vscode并且看了描述。
更新:我终于在打字稿回购中提交了一个错误报告:https://github.com/Microsoft/TypeScript/issues/26675
目前,这不能在JSDoc中完成。
经过一番思考后,我认为核心问题是改变expect类型的语句是一个副作用函数调用,而不是声明。 Typescript没有能力改变那样的类型。
我认为在短期内你最好的选择是使用一个单独的.d.ts文件来表达类型变换作为类型合并,如上所述,尽管如果它们不适合已经合并,可能需要对jest的类型进行一些处理。 。
然而,这对于想要成为纯Javascript的项目来说并不是一个令人满意的解决方案。让我们保持这个问题的开放,以跟踪mut-as-merge jsdoc标记的想法。
资料来源:https://github.com/Microsoft/TypeScript/issues/26675#issuecomment-416952171
如果有人有兴趣,我有关于StackOverflow的跟进问题:How to describe the interface of a simple Just functor in typescript?