microsoft / microsoft/TypeScript
Narrow unnecessarily ambiguous Date.getMonth return type.
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
The specified behaviour of Date.getMonth implies that it will always return a number from 0 - 11 inclusive.
Hence annotating the return type (source) as number is unnecessarily ambiguous.
If it would be accepted, I would like to open a PR to narrow the type down to the 12 values from 0 - 11.
🔍 Search Terms
Date
getMonth
ambiguous type
narrowing type
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- the set of
0..11inclusive is also part of the setnumber. Hence this narrowing is not breaking.
- the set of
- 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
If it would be accepted, I would like to open a PR to narrow the type down to the 12 values from 0 - 11.
interface Date {
getMonth(): 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11
}
📃 Motivating Example
I think this is a very small and self-explanatory enhancement. The benefit here being that the type better describes the behaviour.
Whereas before "the set of things this function could do" was much wider. Now by reading the type (even if you didn't know the name of the method) you could rest assured that you are handling all cases correctly.
Secondly, by looking at the type, one need not read documentation to know that January is 0, not 1 as many JS developers probably learned the hard way.
💻 Use Cases
Make types represent the behaviour of a function more accurately.
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 en lib/lib.es5.d.ts alrededor de la línea 751, donde está vinculada la declaración de Date.getMonth. Compara el tipo de retorno declarado con el comportamiento indicado de 0–11 y revisa el tipo de unión literal propuesto en el issue. Se considera terminado cuando la declaración refleja correctamente todos los valores de mes posibles y las comprobaciones relevantes de TypeScript pasan.
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
- 2/5
- Tiempo estimado
- 1-3 horas
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100