typescript-go icon indicating copy to clipboard operation
typescript-go copied to clipboard

TS2304 when consuming a non-module global namespace object

Open HolgerJeromin opened this issue 3 weeks ago • 2 comments

Steps to reproduce

https://github.com/HolgerJeromin/reproducible-examples/tree/tsgo-2214-namespace

I have a non-module ("module": "None") project which creates a namespace.

// file: nonmodule/nonmodule.ts
namespace myNamespace {
  export const version = "1.0.0";
}
tsconfig.json
{
  "$schema": "http://json.schemastore.org/tsconfig",  
  "compilerOptions": {
    "composite": true,
    "module": "None",
    "target": "ES2022",
    "types": [],

    "strict": true
  },
  "files": ["nonmodule.ts"]
}

With a module I work with this data.

// file: src/main.ts
let _foo = myNamespace.version;
tsconfig.json
{
  "$schema": "http://json.schemastore.org/tsconfig",
  "references": [{"path": "../nonmodule"}],
  "compilerOptions": {
    "module": "es2022",
    "target": "ES2022",
    "types": [],

    "strict": true
  },
  "files": ["main.ts"]
} 

Behavior with [email protected]

Compiles without problems.

Behavior with tsgo

src/main.ts:1:12 - error TS2304: Cannot find name 'myNamespace'.

1 let _foo = myNamespace.version;
             ~~~~~~~~~~~

Found 1 error in src/main.ts:1

HolgerJeromin avatar Dec 04 '25 11:12 HolgerJeromin

"none" as a module setting has been removed, we just haven't errored on it yet.

Are you looking for moduleDetection=legacy?

jakebailey avatar Dec 04 '25 14:12 jakebailey

Thanks for your answer. We use module:none since 10 years. It always felt better to explicit define what we target and not to rely on some guessing and changing the guessing mode for the compiler.

When I change nonmodule\tsconfig.json

-    "module": "None",
+    "moduleDetection": "legacy",

The error is not fixed. Compile of nonmodule project still works like a charm, but the namespace is still not resolved in src folder.

"none" as a module setting has been removed, we just haven't errored on it yet.

Ah. Found #2085 for that.

HolgerJeromin avatar Dec 04 '25 16:12 HolgerJeromin