microsoft / microsoft/TypeScript
Add feature to re-declare variable
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
redeclare variable, re-declare variable, augment variable, merge variable declaration, declaration override
Suggestion
define a redeclare keyword which allows re-declaration of a typed variable.
e.g.
redeclare var foo: {
prop: string
};
Use Cases
When an external library declares a variable, e.g.
declare var x: {
prop: string;
};
The redeclare keyword can be used somewhere else to override the declaration:
redeclare var x: number;
or augment it:
redeclare var x: typeof x & {
augmentedProp: number;
}
This would allow to type the pure JavaScript case of:
// lib.js
var x = {
prop: "foo"
};
// app.js
x.augmentedProp = 10;
console.log(x.prop, x.augmentedProp);
Examples
declare var URLSearchParams: { prototype: URLSearchParams; new(init?: string[][] | Record<string, string> | string | URLSearchParams): URLSearchParams; toString(): string; };defined in
lib.dom.d.ts
to extend the variable with additional static methods to URLSearchParams is not possible currently (see here).
The feature can be used in the following way:
redeclare var URLSearchParams: typeof URLSearchParams & {
fromObject(obj: object): URLSearchParams;
};
if (!URLSearchParams.fromObject) {
URLSearchParams.fromObject = function(obj: object): URLSearchParams { /* implementation */ }
}
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
Beginne mit den vorgeschlagenen redeclare-Beispielen und der bestehenden Deklaration in lib/lib.dom.d.ts, insbesondere mit dem Fall URLSearchParams. Untersuche, wie TypeScript derzeit globale Variablendeklarationen und Erweiterungen behandelt, und definiere dann das Verhalten beim Überschreiben und Zusammenführen von Deklarationen. Als abgeschlossen gilt die Arbeit, wenn der Vorschlag eine festgelegte Semantik und entsprechende Compiler-Tests für die Beispiele enthält.
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