D
Deno

help

How use namespace on Deno

N</Nexus_Prime>4/4/2023
i am trying to use the same namespace in multiple modules but what used to work in node, doesn't work in deno:
/* test1.ts */

/// <reference path="./test2.ts" />

namespace Test {
export const a = 3;
}
/* test1.ts */

/// <reference path="./test2.ts" />

namespace Test {
export const a = 3;
}
/* test2.ts */

/// <reference path="./test1.ts" />

export namespace Test {
export const b = 5;
// If i try to use "a", doesn't work
}
/* test2.ts */

/// <reference path="./test1.ts" />

export namespace Test {
export const b = 5;
// If i try to use "a", doesn't work
}
/* test3.ts */
/// <reference path="./test1.ts" />
/// <reference path="./test2.ts" />

console.log(Test.a); // "Cannot find name 'Test'."
/* test3.ts */
/// <reference path="./test1.ts" />
/// <reference path="./test2.ts" />

console.log(Test.a); // "Cannot find name 'Test'."
MMrKleeblatt4/5/2023
You could and should use modules instead. There is a rule by Deno linting for that: https://lint.deno.land/?q=namespace#no-namespace
/* test1.ts */
export const a = 3;
/* test1.ts */
export const a = 3;
/* test2.ts */
export { a } from "./test1.ts";
export const b = 5;
/* test2.ts */
export { a } from "./test1.ts";
export const b = 5;
/* test3.ts */
import * as Test from "./test2.ts";
console.log(Test.a);
/* test3.ts */
import * as Test from "./test2.ts";
console.log(Test.a);
This works.
/* test2.ts */
import { a } from "./test1.ts";
export default { b: 5, a };
/* test2.ts */
import { a } from "./test1.ts";
export default { b: 5, a };
/* test3.ts */
import Test from "./test2.ts";
console.log(Test.a);
/* test3.ts */
import Test from "./test2.ts";
console.log(Test.a);
This works too.

Looking for more? Join the community!