microsoft / microsoft/TypeScript
Narrow unnecessarily ambiguous Date.getMonth return type.
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia in lib/lib.es5.d.ts intorno alla riga 751, dove è collegata la dichiarazione di Date.getMonth. Confronta il tipo restituito dichiarato con il comportamento indicato di 0–11 ed esamina il tipo union di letterali proposto nell’issue. Il lavoro è completato quando la dichiarazione riflette accuratamente tutti i possibili valori del mese e i controlli TypeScript pertinenti hanno esito positivo.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100