microsoft / microsoft/TypeScript
Catch when callbacks are missing a return.
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con los ejemplos de callbacks del issue y revisa cómo TypeScript comprueba actualmente los callbacks que se pasan a métodos como findIndex. Define el comportamiento del diagnóstico para callbacks con cuerpo de bloque que no devuelven ningún valor, preservando los callbacks con cuerpo de expresión y los retornos falsey explícitos; se considerará terminado cuando los casos solicitados se distingan de forma coherente.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100