question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSDoc @augments doesn’t allow function calls to augment existing types

See original GitHub issue

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

TypeScript Version: 3.0.1 (and 3.1.0-dev.20180825) The description below is for 3.0.1 - for 3.1.0-dev.20180825 all jest functions show errors, so I don’t even think that the parser gets to my type at all.

[ts] Cannot find name 'describe'.
[ts] Cannot find name 'test'.
[ts] Cannot find name 'expect

Used via vscode version: 1.26.1

Search Terms: is:issue is:open jsdoc extends is:issue is:open jsdoc @augments is:issue is:open jsdoc “@augments

I first asked this as a question on StackOverflow but after waiting a few days, I now suspect it is a bug in typescript’s JSDoc implementation.

I also read the FAQ and found the link to jest but didn’t find any information there either.

I’m using plain JavaScript and the code runs perfectly.

Code

// @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}`
      }
    }
  }
})

/**
* @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))
  })
})

Expected behavior: expect().functorToBe should be recognized as a function.

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

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

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

image of vscode intellisense displaying jest.Matchers as return type for expect()

Playground Link:

Related Issues: No.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sandersncommented, Aug 29, 2018

Take a look at Module Augmentation on https://www.typescriptlang.org/docs/handbook/declaration-merging.html. Your second idea of merging with Matchers is basically right, I think, since that’s what is returned by expect. Your index.d.ts would have to reference jest’s index.d.ts via the name your import it as – probably just 'jest'.

0reactions
dotnetCarpentercommented, Aug 29, 2018

Hmm… this doesn’t seem quite right. But I’ll take it to SO and stop polluting this issue.

import { Matchers } from 'jest'

interface Functor<T> {
  (value?: any): {
    fmap: (f: value) => Functor<T>
  }
}

interface Matchers<R> {
  functorToBe(actual: Functor<T>, expected:  Functor<T>): R
}

https://stackoverflow.com/questions/52082800/how-to-describe-the-interface-of-a-simple-just-functor-in-typescript

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use JSDoc: @augments
The @augments or @extends tag indicates that a symbol inherits from, and potentially adds to, a parent symbol. You can use this tag...
Read more >
JSDoc Reference - TypeScript: Documentation
JSDoc Reference. The list below outlines which constructs are currently supported when using JSDoc annotations to provide type information in JavaScript files.
Read more >
JSDoc @augments @typedef not working as expected
According to the JSDoc documentation (http://usejsdoc.org/tags-augments.html), I should be able to define a type...
Read more >
JSDoc typings: all the benefits of TypeScript, with none of the ...
The above code is simple: it defines a function, add , that adds two numbers. Simple JavaScript, no types. Now let's add some...
Read more >
Overview - TypeScript
Additionally, generators just assumed the type of yield was always any . function* bar() { let x: { hello(): void } = yield;...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found