microsoft / microsoft/TypeScript
parseInt: use more concrete type for `radix` argument
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Search Terms
parseInt, radix
Suggestion
From MDN docs:
radix: An integer between 2 and 36 that represents the radix
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#Parameters
TS definition (lib.es5.d.ts):
declare function parseInt(s: string, radix?: number): number;
It will be safer to use more concrete type for radix:
type TRadix =
2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36
function safeParseInt(s: string, radix?: TRadix) {
return parseInt(s, radix)
}
Use Cases
['1', '7', '11'].map(safeParseInt) // error -> Type 'number' is not assignable to type 'TRadix' ("strictFunctionTypes": true)
Examples
['1', '7', '11'].map(item) => safeParseInt(item, 10)) // OK
const config = {
nested: {
number: '42',
radix: 10,
},
} as const;
safeParseInt(config.nested.number, config.nested.radix)) // OK
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.
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 con la dichiarazione di parseInt in lib.es5.d.ts ed esamina come il suo parametro radix è rappresentato nelle definizioni della libreria standard. Controlla quindi gli esempi forniti di safeParseInt e map, poi determina il comportamento a livello di tipi e le aspettative di compatibilità che definirebbero il completamento.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 30/100