Closed as not planned
Description
Before You File a Proposal Please Confirm You Have Done The Following...
- I have searched for related issues and found none that match my proposal.
- I have searched the current extension rule list and found no rules that match my proposal.
- I have read the FAQ and my problem is not listed.
My proposal is suitable for this project
- My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
- I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).
Link to the base rule
https://eslint.org/docs/latest/rules/no-var
Description
Typescript global declarations are more subtle than I ever expected. For a better understanding, I recommend these SO answers:
Here are some key takeaways. If you want to add properties to globalThis
:
- When the typescript file has no
import
orexport
keyword (i.e., it's a script, not a module), it is done like so:declare var age: number
. - When the typescript file does have
import
orexport
, you write this instead:// an import / export line declare global { var age: number; }
If const
or let
is used instead of var
, globalThis.age = 18
would error in both scenarios. So if the intention is to declare globalThis.age
, it is necessary to use the var
keyword: using const
or let
would be a mistake. When enabled, the base no-var rule indiscriminately marks all var
usage as wrong, but in this case, it is required.
Fail
/*eslint no-var: "error"*/
export {}
var x = "y";
var CONFIG = {};
Pass
export {}
declare global {
var age: number;
}
Additional Info
No response