Skip to content

Namespaces: use before declaration is not reported #61891

@yarden-app

Description

@yarden-app

πŸ”Ž Search Terms

Namespace, use before declaration, initialized variable

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried.

⏯ Playground Link

https://www.typescriptlang.org/play/?target=99&jsx=0&module=1&ts=5.9.0-dev.20250618#code/KYDwDg9gTgLgBAOwIYFtgGcxIMbDgQQG8AoOOUSWRVDLXAgRjhLLIunmwgXXgDMEcALxwAFAEphAPjgAiXlACWCAOazScAL7EN7KsjSYcefACYWrPZ2684SKFCQBPYXADa+AMwA6AG5IAGwBXYABdDW1dcA5qQzoTTws2aKouHnh-YLwRfAZvAQkI4k0gA

πŸ’» Code

export namespace A{
  export namespace A1 {
    export const fn = () => "string"
  }

  export namespace A2{
    // should report use before initialization here 
    export const array = [A3.value]
  }

  export namespace A3{
    export const value = A1.fn()
  }
}

πŸ™ Actual behavior

Hi, using typescript V5+, the code provided did not reports any errors, and doesn't work in runtime due to use before initialization.

πŸ™‚ Expected behavior

It is expected that TS2448 and TS2454 will be reported.(use before declaration)
These are reported if the namespace keyword is omitted in the code example and it should be the same when namespaces are used.
It seems files with namespaces are always transpiled with the same format using any TS configuration. (everything becomes iife)

Additional information about the issue

Optimistically additional support for namespace will be added, such as not transforming the entire file to iife when namespaces are used- which happen on any configuration.

And maybe even an option to make a code like I've sent work, by isolating these namespaces properly, and resort their order by their variable dependencies between themselves, and report when cycling variable dependency between namespaces exists.

Activity

added this to the Backlog milestone on Jun 18, 2025
azerum

azerum commented on Jun 23, 2025

@azerum

Simpler example where code compiles, but fails at runtime:

function f(n: number) {
    return n.toFixed(2)
}

const theAnswer = f(Foo.answer)

namespace Foo {
    export const answer = 42.001
}

Which seems surprising as using class instead of namespace does report the error. I wonder what makes them different with respect to type checking:

function f(n: number) {
    return n.toFixed(2)
}

// Class 'Foo' used before its declaration. ts(2449)
const theAnswer = f(Foo.answer)

class Foo {
    static answer = 42.001
}
copilot-swe-agent

Copilot commented on Jun 23, 2025

Copilot
yarden-app

yarden-app commented on Jun 23, 2025

@yarden-app
Author

I purposely use that code example to complement my "optimistic" request. This will really help with the semantic of the module/file. Like how you would declare greater functions before lesser one in the same file.

I don't want to use ambient namespace and want to avoid declare keyword. Both which do not support nesting properly, which is kind of the main thing about namespace anyway.

Thanks

copilot-swe-agent

Copilot commented on Jun 23, 2025

Copilot
Contributor

@RyanCavanaugh Unfortunately I hit an unexpected error while trying to work on issue #61891. I've automatically reported this to GitHub.

You can ask me to try again later by unassigning and then reassigning the issue to me again.

If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: 19689387-b3ca-4037-9cb2-b3fb4268874a

Sorry for the inconvenience!

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

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: check: Control FlowThe issue relates to control flow analysisFix AvailableA PR has been opened for this issueHelp WantedYou can do this

Type

No type

Projects

No projects

Relationships

None yet

    Development

    Participants

    @RyanCavanaugh@typescript-bot@yarden-app@azerum

    Issue actions

      Namespaces: use before declaration is not reported Β· Issue #61891 Β· microsoft/TypeScript