microsoft / microsoft/TypeScript
Index signature alternative
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
- Index signature alternative
- Index signature
Suggestion
An alternative of Index signature would be beneficial when you want to enforce, that all properties of an object must follow given pattern, without the need of [key: string | number]: T1.
My suggestion is to enable the use of ? for index signatures to mark them as type guards only.
type posParams = {
[name?: string]: UrlSegment;
};
Use Cases
Whenever there is a need for an object with unknown keys, but known value types this would come in handy. A lot of people trying to implement some sort of callback that requires the index signature fails.The enormous number of results if one googles Index signature is missing in type confirms this
I personally tend to confront this problem regularly when working with frameworks..
Examples
For this example, I will use Angular, but this is applicable to any typescript code.
type UrlMatchResult = {
consumed: UrlSegment[];
posParams?: {
[name?: string]: UrlSegment;
};
};
When one would be to implement it with only specific posParams it would only check for the type of the object keys and values.
export interface IUrlParameters {
baseFolder: UrlSegment;
currentFolder?: UrlSegment;
}
export function folderMatcher(segments: UrlSegment[]): UrlMatchResult {
const posParams: IUrlParameters = {
currentFolder: segments[0],
};
return {
consumed: segments,
posParams,
}
}
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
Das Issue schlägt eine optionale Syntax für Index-Signaturen zur Prüfung bekannter Eigenschaftswerte vor, mit Beispielen unter Verwendung von UrlSegment und Angular. Es werden keine Implementierungsdateien, Einstiegspunkte oder Tests genannt; beginne damit, TypeScripts Typprüfung für Index-Signaturen und Parser-Tests zu finden. Als abgeschlossen gilt die Erarbeitung einer Designentscheidung und die Abdeckung des akzeptierten Verhaltens durch Tests.
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
- 25/100