Add feature to re-declare variable
See original GitHub issueSearch 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.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top Results From Across the Web
how to redeclare a variable in different datatypes in c
No, you cannot redefine a variable with different types within the same scope. Although, there is one possible alternative.
Read more >Understand variables in canvas apps - Power Apps
All variables are created implicitly when they appear in a Set, UpdateContext, Navigate, Collect, or ClearCollect function. To declare a ...
Read more >Declare a variable | Help - Zoho Deluge
Assigning a new value to a variable declared earlier will overwrite the existing value. · Variables need not be declared initially before assigning...
Read more >Documentation: 15: 43.3. Declarations - PostgreSQL
%TYPE provides the data type of a variable or table column. You can use this to declare variables that will hold database values....
Read more >Procedural language | BigQuery - Google Cloud
You do not need to declare system variables, but you can set any of them that ... add a row for Ulysses, using...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@DanielRosenwasser my answer would be:
hello
would end up astring
(if the code is processed sequentially)redeclare
to be scoped, so if the variable was redeclared within a function body (or any other relevant scope), the redeclaration would be valid for the scope of the function body. e.g for an express app:redeclare
can have other benefits:I have a “header” type of interface:
I want to have extended interface:
if there is no inex type in
api_msg_t
the typeguardis_api_node_online
fails to transpile asm.nid
does not exist inapi_msg_t
. But adding index in theapi_msg_t
propagates it toapi_node_online_t
that is undesirable. if we can doIt will allow for elegant and typesafe solution.
We can easily mimic this behavior with
but this results in unnecessary JS code emitted and ran