Skip to content

A type can be named undefined  #57573

Closed

Description

🔎 Search Terms

type named undefined

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ

⏯ Playground Link

Playground Link

💻 Code

namespace ns {
    export type undefined = "";
    export function x(p: undefined): undefined { // global undefined
        
    }
}

function x(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)

Additional information about the issue

No response

Activity

  1. fatcerberus commented on Feb 28, 2024

    @fatcerberus

    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).

  2. dragomirtitian commented on Feb 28, 2024

    @dragomirtitian
    ContributorAuthor

    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.

    We could allow it. But it'd rather ban it 😅

  3. added
    Suggestion
    Experience Enhancement
    on Feb 28, 2024
  4. RyanCavanaugh commented on Feb 28, 2024

    @RyanCavanaugh
    Member

    I can't imagine anyone doing this on purpose except to be evil... banning seems like the right call

  5. jakebailey commented on Feb 28, 2024

    @jakebailey
    Member

    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?

  6. jakebailey commented on Feb 28, 2024

    @jakebailey
    Member

    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

  7. dragomirtitian commented on Feb 28, 2024

    @dragomirtitian
    ContributorAuthor

    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

  8. jakebailey commented on Feb 28, 2024

    @jakebailey
    Member

    Ah, there's the error I was looking for. Thanks.

  9. RyanCavanaugh commented on Feb 28, 2024

    @RyanCavanaugh
    Member
    export type undefined = "";
    let m: undefined = "";
    // error^

    If it's not going to work, we shouldn't let you do it

  10. reverofevil commented on Jul 6, 2024

    @reverofevil

    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.

  11. jakebailey commented on Jul 6, 2024

    @jakebailey
    Member

    Did anyone check this time there are no other such identifiers?

    Yes: #57575 (comment)

  12. jakebailey commented on Jul 6, 2024

    @jakebailey
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Experience EnhancementSuggestion

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions