microsoft / microsoft/TypeScript
Add feature to re-declare variable
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con los ejemplos propuestos de redeclare y la declaración existente en lib/lib.dom.d.ts, especialmente con el caso de URLSearchParams. Revisa cómo gestiona actualmente TypeScript las declaraciones de variables globales y las ampliaciones, y luego define el comportamiento para sobrescribir y combinar declaraciones. Se considerará terminado cuando la propuesta haya establecido la semántica y existan las pruebas correspondientes del compilador para los ejemplos.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 25/100