Comments (4)
I think what you should actually be seeing here is a circularity error
from typescript.
let invalid1 = 1
doesn't define globalThis.invalid1
, so there is no circularity going on (it only happens with var
).
Note that the code above has actually valid use cases. I spotted it while doing
let SuppressedError = globalThis.SuppressedError ?? class SuppressedError extends Error {
// ...
}
from typescript.
Hello @nicolo-ribaudo.
Attempting to use the variable before declaration will result in a ReferenceError. This is known as the "temporal dead zone," where the variable exists in the scope but cannot be accessed.
But, When you declare a variable with var
, the variable declaration is "hoisted" (or "raised") to the top of your current scope. This means that, regardless of where you declared the variable in the code, JavaScript treats it as if you had declared it at the beginning of the scope. However, only the declaration is hoisted, not the initialization. Therefore, if you try to access the variable before initializing it, the value will be undefined
, but it will not cause an error.
On the other hand, when you declare a variable with let
, the declaration is also hoisted, but JavaScript puts these variables in a "temporal dead zone" from the start of the block until the declaration is processed. During this "dead zone", if you try to access the variable, you will receive a ReferenceError.
So, in your case, when you try to do let invalid1 = globalThis.invalid1;
without globalThis.invalid1
being defined, invalid1
is undefined
. This is not a problem in itself, but if you try to access invalid1
somewhere in the code before this line, you will receive a ReferenceError because of the "temporal dead zone" of let
.
However, if you use var
instead of let
, as in var invalid1 = globalThis.invalid1;
, even if globalThis.invalid1
is not defined, invalid1
will simply be undefined
and will not cause an error, even if you access invalid1
before this line, thanks to the hoisting of var
.
from typescript.
In the example above no variables are in temporal dead zone -- you can try running that code and it does not error.
from typescript.
Related Issues (20)
- Bloomberg TS5.5-beta feedback HOT 1
- Conditional Type Inference Bug in TS 5.x
- Protected instance member inaccessible to same-class constructor via object destructuring assignment HOT 2
- [ERROR] 15:13:55 Error: Cannot set headers after they are sent to the client HOT 3
- Isolated declarations does not handle object getters and setters with different types HOT 5
- TypeScript does not support bigint literals in type literal keys HOT 1
- Isolated declarations uses type information when emitting computed object keys HOT 5
- Error: Debug Failure when importing `AssertionError` from `node:assert/strict` HOT 4
- transpileDeclaration - Errors on unresolvable globals HOT 2
- duplicate comments on decorated fields
- Type narrowing with union types create impossible case with else statement HOT 6
- Error when using `import()` type within JSDoc tag `@implements`
- Narrowring a readonly class with instanceof of does not work on the else branch HOT 1
- Disposables disposed of, even though they are passed to a wrapper and still in use HOT 3
- It's the JSON validator that complains here. HOT 3
- Isolated declarations inconsistent about banning default assignment patterns for function parameters
- Isolated declarations uses type information to emit return type for async function HOT 2
- Inconsistency with Rest Parameter in Function Type HOT 2
- 2024 便宜机场推荐:便宜机场整理
- Why is Promise<void> accepted as void HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript.