microsoft / microsoft/TypeScript
Distributive as const
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
as const, widening, literal widening
Suggestion
Allow to use as const on ternary operator as well as others code branches that currently we can't.
Use Cases
Let's say that we have this code:
const result = (
value > 5
? { amount: 'many' } as const
: { amount: 'few' } as const
)
We need to type as const twice. So would be nice if we could type just once:
const result = (
value > 5
? { amount: 'many' }
: { amount: 'few' }
) as const
Currently we can't do that (playground).
It's just a syntax sugar. It won't change anything on as const!
Analogously, maybe we could allow this syntax sugar on function:
const func = (param: number) => {
if (param > 5) {
return 'many' as const
}
return 'few' as const
}
// would be just...
const func = (param: number): const => {
if (param > 5) {
return 'many'
}
return 'few'
}
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 el ejemplo enlazado de TypeScript Playground para reproducir la colocación rechazada de as const y compárala con la forma aceptada a nivel de rama. No se nombran archivos fuente ni pruebas; identifica los puntos de entrada del compilador y de las pruebas para const assertions y, después, verifica que la sintaxis ternaria propuesta se acepta sin cambiar el JavaScript emitido ni el comportamiento existente.
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