microsoft / microsoft/TypeScript

Catch when callbacks are missing a return.

Aperta
#36,472 5 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs Proposal Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Search Terms

  • array callback return
  • eslint array-callback-return
  • catch missing return for callbacks

Suggestion

TypeScript does not recognise this as an error even though it clearly is - findIndex takes a callback which should return a boolean, returning an (implicit) undefined is IMO in 99% of cases a mistake.

[1,2,3].findIndex(e => {
  e === 1
})

I also think that if there is an explicit return of a falsey value such as null or undefined, then that should be fine. It's the lack of any return at all which should trigger an error, in my eyes.

I'm aware that ESLint can do this but not everyone uses ESLint, also ESLint can be a huge pain to configure sometimes, especially when Prettier, TypeScript and other things are involved.

Use Cases

It will definitely catch errors, IMHO, for people who don't use ESLint or do not have the rule array-callback-return.

Examples

No error:

[1,2,3].findIndex(e => 
  e === 1
)

No error:

[1,2,3].findIndex(e => e === 1)

A warning/errror: "Did you forget a return?"

[1,2,3].findIndex(e => {
  e === 1
})

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 con gli esempi di callback nell’issue e verifica come TypeScript controlla attualmente i callback passati a metodi come findIndex. Definisci il comportamento diagnostico per i callback con corpo a blocco senza return, preservando i callback con corpo a espressione e i return espliciti di valori falsey; il lavoro è completato quando i casi richiesti vengono distinti in modo coerente.

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
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.