microsoft / microsoft/TypeScript

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

Aperta
#26,675 7 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: JavaScript Domain: JSDoc Experience Enhancement Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo l’esempio fornito in semplice JavaScript con TypeScript 3.0.1 o typescript@next, concentrandoti sulla gestione di JSDoc @extends/@augments attorno a jest.Matchers. Il lavoro è completato quando expect(just(1)).functorToBe(just(1)) supera il controllo dei tipi senza l’errore di proprietà mancante e le funzioni Jest vengono riconosciute.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.