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:
[lint.rules]
"import/namespace" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"import/namespace" = ["error", {
allowComputed = false,
}]Examples#
Reported:
export function save(item) {}
export function load(id) {}import * as api from "./api.js";
api.removeremove not found in imported namespace api.("draft");
const { updateupdate not found in imported namespace api. } = api;
api.save = () => {}Assignment to member of namespace api.;
const action = "load";
api[actionUnable to validate computed reference to imported namespace api.]("draft");Not reported:
export function save(item) {}
export function load(id) {}import * as api from "./api.js";
api.save({ id: "draft" });
const { load } = api;
load("draft");Options#
| Option | Default | What it does |
|---|---|---|
allowComputed | false | Allow computed reads such as api[key]. They aren't checked. |
export function save(item) {}
export function load(id) {}{ "allowComputed": true }No problemsimport * as api from "./api.js";
const action = "load";
api[action]("draft");