JSDoc文档对象方法扩展其他类 [英] JSDoc document object method extending other class

查看:260
本文介绍了JSDoc文档对象方法扩展其他类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了 jest.Matchers 的简单扩展名,但是我无法获得打字稿类型检查器以识别我的扩展名.

I wrote a simple extension of jest.Matchers but I can not get the typescript type checker to recognise my extension.

我使用的是纯JavaScript.

I'm using plain 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下得到红色下划线,并显示以下错误消息:

But in the line with expect(just(1)).functorToBe(just(1)), I get a red underline under functorToBe and the following error message:

[ts]类型'Matchers< {[x:string]:any;不存在属性'functorToBe' fmap:(f:任何)=>任何; }>'. 任何

[ts] Property 'functorToBe' does not exist on type 'Matchers<{ [x: string]: any; fmap: (f: any) => any; }>'. any

我在 vscode 中写了expect()后得到了jest.Matchers,然后看了一下描述.

I got jest.Matchers from writing expect() in vscode and looked at the description.

更新:我终于在打字稿存储库中为此提交了一个错误报告: https://github.com/Microsoft/TypeScript/issues/26675

Update: I've finally filed a bug report in the typescript repo for this: https://github.com/Microsoft/TypeScript/issues/26675

推荐答案

当前无法在JSDoc中完成.

Currently this can not be done in JSDoc.

经过进一步思考,我认为核心问题是,更改Expect类型的语句是一个副作用函数调用,而不是声明. Typescript没有能力对这样的类型进行突变.

After a bit more thought, I think the core problem is that the statement that changes the type of expect is a side-effecting function call, not a declaration. Typescript doesn’t have the ability to mutate types like that.

我认为短期内最好的选择是使用一个单独的.d.ts文件将类型突变表示为类型合并,如我上面概述的那样,尽管如果不合理,可能需要对Jest的类型进行一些工作合并.

I think your best bet in the near term is to use a separate .d.ts file to express the type mutation as a type merge as I outlined above, although that may require some work on jest’s types if they are not amenable to merging already.

但是,对于想要使用纯Javascript的项目来说,这不是令人满意的解决方案.让我们保持开放状态,以跟踪突变合并" jsdoc标记的想法.

This not a satisfying solution for projects that want to be pure Javascript, however. Let’s keep this issue open to track the idea of a mutation-as-merge jsdoc tag.

来源: https://github.com/Microsoft/TypeScript/issues /26675#issuecomment-416952171

如果有人感兴趣,我会对StackOverflow进行跟进:

I have a follow up question on StackOverflow if anyone is interested: How to describe the interface of a simple Just functor in typescript?

这篇关于JSDoc文档对象方法扩展其他类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆