microsoft / microsoft/TypeScript

JSDoc missing syntax for `new Map<string, string>()`

Aperta
#38,876 3 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

TypeScript Version: 3.7.x-dev.201xxxxx

Search Terms: JSDoc Map generics assignment any unsafe

Code

class MyServer {
  constructor () {
    // new Map is Map<any, any>; cannot call Map constructor with new Map<string, string[]>
    /** @type {Map<string, string[]>} */
    this.functions = new Map()
  }
}

const myServer = new MyServer()
myServer.functions.get('foo')

Expected behavior:

Expected to be able to say what the Map contains when creating a new map. In typescript you can do new Map<string, string[]>() but theres no explicit generic parameter function invocation syntax for JSDoc

Actual behavior:

The type Map<any, any> exists. This fails the eslint rule of no-unsafe-assignment and this fails the no-any rule of tslint etc.

Note that doing something similar for arrays works because there's a special case inference for [] and it also works for Set because the constructor is defined differently and it has generic inference in the initialization statement.

Playground Link: https://www.typescriptlang.org/play?useJavaScript=true#code/MYGwhgzhAECyCeBlApgJwG5ugbwFDWmAHsA7CAF1QFdhyjVoAKASh3wOgHoAqb6AAXLwADshywwwgDwVUASxIBzADTRZCxQG0AugD4AvtG6d2BcgAs5EAHQAzKiVpzSMALzQSyAO5xJLdvq4gbjEZOTQALZIaJgM7p4+CCgYaP5RybF2Dk4u1orI5IwA5LZEREXMuCHgUHDRKQx4BKGyNHQMLGwcXLwCQqI4AIKoqGDwMpQaBkYm3RZWWY7kzmTQ7joBQVUt4ekxWPHedRmplXsNizlkeQXFpeXMQA

Related Issues: Yes, in eslint https://github.com/typescript-eslint/typescript-eslint/issues/2109

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

Riproduci l’esempio JavaScript nel TypeScript Playground collegato e confronta il risultato attuale di Map<any, any> con l’assegnazione tipizzata prevista. Esamina come i tipi JSDoc rappresentano le chiamate a costruttori generici, usando come contesto la issue correlata di typescript-eslint. Il lavoro è completato quando viene definita una sintassi supportata e l’esempio non inferisce più un tipo any non sicuro.

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

Valutazione

Stack tecnologico
javascript, typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.