microsoft / microsoft/TypeScript
Check truthy/falsy-ness for non-primitives to identify unreachable code
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
Suggestion
In some cases TypeScript can identify unreachable code and throws an error:
const isTrue = true;
if (isTrue === false) {
console.log('Can never be reached.')
}
In other cases it is currently not possible, but it would be very helpful. (Maybe even with a suggestion what could have been meant - see array example.)
const isArray = ['hello'];
if (!isArray) {
console.log('Can never be reached. You probably meant `if (!isArray.length) {`.')
}
const isObject = {};
if (!isObject) {
console.log('Can never be reached.')
}
🔍 Search Terms
- reachable / unreachable
- dead code
- condition
- falsy / truthy
- primitive / non-primitive
- always
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code -> It can break code, but only to show potential bugs.
- 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, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
Throw errors on unreachable code examples.
const isArray = ['hello'];
if (!isArray) { // error: This condition will always return false. Did you meant to check `isArray.length`?
console.log('Can never be reached. You probably meant `if (!isArray.length) {`.')
}
const isObject = {};
if (!isObject) { // error: This condition will always return false.
console.log('Can never be reached.')
}
// OR:
if (isArray) {
} else {
console.log('Can never be reached. You probably meant `if (!isArray.length) {`.')
}
📃 Motivating Example
This week I saw a colleague which could have found a bug way sooner by introducing such a check. ❤️
💻 Use Cases
Finding bugs.
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 revisando los diagnósticos existentes de TypeScript para código inalcanzable y condiciones, y luego compara su comportamiento con los ejemplos de arrays y objetos del issue. Define cómo deberían notificarse los no primitivos truthy/falsy y si la orientación sugerida sobre length es adecuada; se considera terminado cuando los ejemplos producen los diagnósticos previstos sin cambiar el JavaScript emitido.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript, 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
- 25/100