Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
Closed as not planned
Description
Before You File a Proposal Please Confirm You Have Done The Following...
- I havesearched for related issues and found none that match my proposal.
- I have searched thecurrent extension rule list and found no rules that match my proposal.
- I haveread 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 toglobalThis
:
- 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 linedeclare global{varage:number;}
Ifconst
orlet
is used instead ofvar
,globalThis.age = 18
would error in both scenarios. So if the intention is to declareglobalThis.age
, it isnecessary to use thevar
keyword: usingconst
orlet
would be a mistake. When enabled, the baseno-var rule indiscriminately marks allvar
usage as wrong, but in this case, it is required.
Fail
/*eslint no-var: "error"*/export{}varx="y";varCONFIG={};
Pass
export{}declare global{varage:number;}
Additional Info
No response