microsoft / microsoft/TypeScript

Suggestion: noInferredAny

Aperta
#39,633 4 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs Proposal Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Search Terms

noImplicitAny inference any noInferredAny

Suggestion

Symbols typed as any reduce the type safety of a program. That's fair if the programmer intended to use any. However it's relatively easy to end up with symbols typed any through type inference chains that are not obvious:

  • 3rd party libraries might return any
  • complex type expressions might infer to any
  • some .d.ts types default generic arguments to any (e.g. Set)

In all these situations, a programmer might write const foo = something(); and expect foo to have a reasonable inferred type. foo being inferred to any is easy to miss in such code, both while editing and while reviewing code.

Proposal: add a compiler option noInferredAny that flags symbols whose type is any and that do not have an explicit type annotation.

Use Cases

  • better type safety for programs
  • detect weak typings in your dependencies
  • more

Examples

const foo1 = returnsAny();  // error
const foo2: any = returnsAny(); // ok
const foo3: string = returnsAny(); // probably ok, programmer gave an explicit type? could also require an explicit cast.
const foo4 = returnsAny() as string;  // ok

const {foo5}: {foo5: string} = returnsAny(); // ok

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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 dalla proposta e dai relativi esempi, in particolare dalla distinzione tra any inferito e any annotato esplicitamente. Esamina i casi di inferenza elencati e definisci come deve comportarsi l’opzione per ciascuno; il lavoro è completato quando il compilatore può segnalare in modo coerente i simboli non annotati inferiti come any senza modificare il JavaScript emesso.

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

Valutazione

Stack tecnologico
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
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.