microsoft / microsoft/TypeScript
Add feature to re-declare variable
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
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.
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 gli esempi proposti di redeclare e la dichiarazione esistente in lib/lib.dom.d.ts, in particolare il caso URLSearchParams. Esamina come TypeScript gestisce attualmente le dichiarazioni di variabili globali e le estensioni, quindi definisci il comportamento per la sovrascrittura e l'unione delle dichiarazioni. Il lavoro sarà considerato completato quando la proposta avrà definito la semantica e saranno presenti test del compilatore corrispondenti per gli esempi.
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
- 25/100