microsoft / microsoft/TypeScript
parseInt: use more concrete type for `radix` argument
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginnen Sie mit der Deklaration von parseInt in lib.es5.d.ts und überprüfen Sie, wie der radix-Parameter in den Definitionen der Standardbibliothek dargestellt ist. Prüfen Sie anschließend die bereitgestellten Beispiele für safeParseInt und map und bestimmen Sie dann das Verhalten auf Typebene sowie die Kompatibilitätserwartungen, die den Abschluss definieren würden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 30/100