You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
namespacens{exporttypeundefined="";exportfunctionx(p: undefined): undefined{// global undefined}}functionx(p: ns.undefined){// undefined from the namespace}
🙁 Actual behavior
Inside the namespace undefined is the global undefined type. We can access the type if we qualify it with the namespace
🙂 Expected behavior
undefined should not be allowed as a type name. (null is not)
undefined should not be allowed as a type name. (null is not)
Amusingly, this matches runtime behavior: null is a keyword, while undefined is not--it's a global variable that can be freely shadowed, and prior to ES5 you could actually mutate the global one (which is why void 0 became popular).
Bruce Pascoe (@fatcerberus) It does partially match that. Except in ES5 if you defined undefined it would use that in the context. This allows you to define it but then ignores it.
On second look, what is the problem here per se? Whenever you write undefined in type space, you always get the real undefined, even inside the namespace. It never emits "wrong", because that undefined (even when inferred) always points to the global undefined.
If you do this outside of a namespace, it will say no, but inside it's just a symbol that can only be accessed from the namespace, no other way, so this doesn't seem harmful, just... weird?
On second look, what is the problem here per se? Whenever you write undefined in type space, you always get the real undefined, even inside the namespace. It never emits "wrong", because that undefined (even when inferred) always points to the global undefined.
If you do this outside of a namespace, it will say no, but inside it's just a symbol that can only be accessed from the namespace, no other way, so this doesn't seem harmful, just... weird?
But it feels very strange that you have a local type named undefined, which regular scoping rules would dictate would take precedence over the global undefined and yet it does not.
Erroring on undefined would also make it similar to what happens for other keyword types such as number Playground Link
Did anyone check this time there are no other such identifiers? Is there anything like identifier contingency plan, such as refactoring the parser to support precisely defined thought-through identifier classes?
I suspect this might either not be fixed, or will break again soon.
Is there anything like identifier contingency plan, such as refactoring the parser to support precisely defined thought-through identifier classes?
I suspect this might either not be fixed, or will break again soon.
undefined is the special case as it's not actually a keyword, but a predeclared identifier in JS but a keyword in type space, which is why this wasn't caught.
Activity
fatcerberus commentedon Feb 28, 2024
Amusingly, this matches runtime behavior:
nullis a keyword, whileundefinedis not--it's a global variable that can be freely shadowed, and prior to ES5 you could actually mutate the global one (which is whyvoid 0became popular).dragomirtitian commentedon Feb 28, 2024
Bruce Pascoe (@fatcerberus) It does partially match that. Except in ES5 if you defined
undefinedit would use that in the context. This allows you to define it but then ignores it.We could allow it. But it'd rather ban it 😅
RyanCavanaugh commentedon Feb 28, 2024
I can't imagine anyone doing this on purpose except to be evil... banning seems like the right call
jakebailey commentedon Feb 28, 2024
On second look, what is the problem here per se? Whenever you write
undefinedin type space, you always get the real undefined, even inside the namespace. It never emits "wrong", because that undefined (even when inferred) always points to the global undefined.If you do this outside of a namespace, it will say no, but inside it's just a symbol that can only be accessed from the namespace, no other way, so this doesn't seem harmful, just... weird?
jakebailey commentedon Feb 28, 2024
Hm, no, it won't say no outside of a namespace. Just error when it's also a global. https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBDAnmYcCuA7AJsAZgSw2CzgF44AiCgbgFgAoIA
dragomirtitian commentedon Feb 28, 2024
But it feels very strange that you have a local type named
undefined, which regular scoping rules would dictate would take precedence over the globalundefinedand yet it does not.Erroring on
undefinedwould also make it similar to what happens for other keyword types such as number Playground Linkjakebailey commentedon Feb 28, 2024
Ah, there's the error I was looking for. Thanks.
RyanCavanaugh commentedon Feb 28, 2024
If it's not going to work, we shouldn't let you do it
reverofevil commentedon Jul 6, 2024
Related #43215
Did anyone check this time there are no other such identifiers? Is there anything like identifier contingency plan, such as refactoring the parser to support precisely defined thought-through identifier classes?
I suspect this might either not be fixed, or will break again soon.
jakebailey commentedon Jul 6, 2024
Yes: #57575 (comment)
jakebailey commentedon Jul 6, 2024
undefinedis the special case as it's not actually a keyword, but a predeclared identifier in JS but a keyword in type space, which is why this wasn't caught.