import/namespace

Require each property read from an import * as namespace to be exported by that file

Also called import-x/namespace

With import * as api from "./api", reading a name that ./api doesn't export gives undefined, and nothing fails until you call it. Assigning to api.save throws, because a namespace object is read-only.

The rule reads the imported file and checks api.name, const { name } = api and <api.Name />. A computed read like api[key] can't be checked, so it's reported too unless you allow it.

It reads JavaScript and TypeScript files, including JSX. With an ESLint config it only reads .js, .mjs and .cjs files, unless settings["import/extensions"] lists others.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"import/namespace" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"import/namespace" = ["error", {
  allowComputed = false,
}]

Examples#

Reported:

api.js
export function save(item) {}
export function load(id) {}
example.js4 problems
import * as api from "./api.js";

api.remove("draft");
const { update } = api;
api.save = () => {};

const action = "load";
api[action]("draft");

Not reported:

api.js
export function save(item) {}
export function load(id) {}
example.jsNo problems
import * as api from "./api.js";

api.save({ id: "draft" });
const { load } = api;
load("draft");

Options#

OptionDefaultWhat it does
allowComputedfalseAllow computed reads such as api[key]. They aren't checked.
api.js
export function save(item) {}
export function load(id) {}
example.jswith { "allowComputed": true }No problems
import * as api from "./api.js";

const action = "load";
api[action]("draft");