【问题标题】:JSDoc document object method extending other class扩展其他类的 JSDoc 文档对象方法
【发布时间】:2019-01-29 00:50:10
【问题描述】:

使用 JSDoc 扩展 jest.Matchers 的文档

我写了一个简单的 jest.Matchers 扩展,但我无法让 typescript 类型检查器识别我的扩展。

我使用的是纯 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 any; }>'。 任何

我通过在vscode 中写入expect() 得到jest.Matchers,并查看了描述。

更新:我终于在 typescript repo 中提交了一个错误报告:https://github.com/Microsoft/TypeScript/issues/26675

【问题讨论】:

  • 我刚刚找到Using Jest with TypeScript。也许会有一些东西可以帮助解决这个问题。
  • 不,那里什么都没有。安装打字稿等不会改变任何东西。

标签: javascript jestjs jsdoc


【解决方案1】:

目前在 JSDoc 中无法做到这一点。

再想一想,我认为核心问题是改变expect类型的语句是一个副作用函数调用,而不是一个声明。 Typescript 无法像那样改变类型。

我认为您近期最好的选择是使用单独的 .d.ts 文件将类型突变表示为我上面概述的类型合并,尽管如果它们不适合 jest 类型,则可能需要对它们进行一些工作已经合并了。

但是,对于想要纯 Javascript 的项目来说,这不是一个令人满意的解决方案。让我们保持这个问题开放,以跟踪突变合并 jsdoc 标记的想法。

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

如果有人感兴趣,我有一个关于 StackOverflow 的后续问题:How to describe the interface of a simple Just functor in typescript?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 2012-01-17
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 2016-07-29
    相关资源
    最近更新 更多