microsoft / microsoft/TypeScript
Allow usage of `Reflect.get()` by returning `unknown` instead of `any`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
Search Terms
Reflect get unknown
Suggestion
Change the type definition of Reflect.get() to return unknown instead of any.
Use Cases
Allow type safe dynamic access of fields on an object or { [k: string}: unknown }
Using Reflect.get(o, 'some key') and have it return unknown.
This allows usage of Reflect.get() in code bases where we do not want implicit any and where we have @typescript-eslint rules to ban unsafe usage of any.
Examples
/**
* @param {CustomError} cause
* @returns {{ [k: string]: unknown }}
*/
function getInfoForPlainError (cause) {
/** @type {{ [k: string]: unknown }} */
const info = {}
for (const field of PLAIN_ERROR_FIELDS) {
const v = reflectGet(cause, field)
if (typeof v !== 'undefined') {
info[field] = v
}
}
return info
}
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.
Unfortunately changing a signature from any to unknown is a breaking change.
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
Beginne damit, die lib.d.ts-Deklaration für Reflect.get und die Tests zu finden, die dessen inferierten Rückgabetyp abdecken. Prüfe, wie sich die Änderung von any zu unknown auf bestehende Erwartungen an die Typprüfung auswirkt; abgeschlossen ist die Aufgabe, wenn die Deklaration und die relevanten Tests den angeforderten Rückgabetyp konsistent unterstützen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100