Skip to content

no-unchecked-declare ​

An ambient declaration that says a value exists, with a type, and leaves both unchecked: a cast with no as. On by default, as an error.

Why: declare const config: Config compiles every read of config.port, and nothing ever looks at what is there: a misconfigured bundler define, a page script that didn't run, or a library whose API moved on all type-check. The honest form says what is known (that a name exists) and checks the rest where the value enters.

❌ Rejected: a value and a function asserted to exist

ts
type Config = { readonly port: number };

declare const injected: Config;
declare function lookup(key: string): Config;

export function port(): number {
  return injected.port + lookup("api").port;
}

❌ Rejected: a member added to a built-in as always there

ts
type Config = { readonly port: number };

declare global {
  interface Window {
    readonly appConfig: Config;
  }
}

export function port(): number {
  return window.appConfig.port;
}

✅ Accepted: only the name is asserted; the value is validated where it is read

✓ Accepted0 diagnostics
ts
import { type JsonError, validate } from "@evobug_com/safe-ts-std/json";
import type { Result } from "@evobug_com/safe-ts-std/result";

type Config = { readonly port: number };

// Replaced by the bundler's `define`.
declare const __APP_CONFIG__: unknown;

export function config(): Result<Config, JsonError> {
  return validate<Config>(__APP_CONFIG__);
}
ts
import { parseJsonWith as __jsonParse, validateWith as __jsonValidate, jsonFail as __jsonFail, isJsonErr as __jsonIsErr, isJsonObject as __jsonIsObject, type JsonResult as __JsonResult } from "@evobug_com/safe-ts-std/json"; import { type JsonError, validate } from "@evobug_com/safe-ts-std/json";
import type { Result } from "@evobug_com/safe-ts-std/result";

type Config = { readonly port: number };

// Replaced by the bundler's `define`.
declare const __APP_CONFIG__: unknown;

export function config(): Result<Config, JsonError> {
  return __jsonValidate<Config>(__APP_CONFIG__, __v0);
}
// ---- generated by safe-ts: JSON validators ----
type __T0 = { "port": number; };
function __v0(v: unknown, p: string): __JsonResult<__T0> {
  if (!__jsonIsObject(v)) return __jsonFail(p, "object", v);
  const __j1 = v["port"];
  if (typeof __j1 !== "number") return __jsonFail(p + ".port", "number", __j1);
  const __j2 = __j1;
  const out: { "port": number; } = { "port": __j2 };
  return out;
}

✅ Also accepted: a global the project sets itself, typed so that it may be missing

✓ Accepted0 diagnostics
ts
import type { Option } from "@evobug_com/safe-ts-std/result";

type Harness = { readonly gpu: boolean };

declare global {
  var harness: Option<Harness>;
  interface Window {
    boot?: (fraction: number) => void;
  }
}

globalThis.harness ??= { gpu: true };

export function gpu(): boolean {
  return globalThis.harness?.gpu === true;
}

✅ Also accepted: Vite's env variables, which may be missing, typed so (with vite/client's types, which declare import.meta.env)

ts
declare global {
  interface ImportMetaEnv {
    readonly VITE_API_URL?: string;
  }
}

export function apiUrl(): string {
  return import.meta.env.VITE_API_URL ?? "/api";
}

❌ Rejected: a field said to be set by something TypeScript doesn't see, field!: by another name

ts
export class Session {
  declare token: string;

  header(): string {
    return `Bearer ${this.token}`;
  }
}

Every ambient declaration is counted in safe-ts check's escape-hatch line (ambient declaration(s); ambientDeclarations in --json --summary), the accepted ones too, and a baseline holds the count down. no-type-predicate and no-overload leave declare forms and declaration files alone because those types describe code defined elsewhere; that is why they are counted here: nothing checks the description.

What is reported, and the checked form ​

writtenreported whenthe checked form
declare const / let / var x: TT is anything but unknown or a type that admits undefined (Option<T>, T | undefined)declare const x: unknown and validate<T>(x) where it is read; a global the project sets is typed Option<T>
declare function f(...), declare class Calwaysread it as unknown and trust<(x: A) => R>(f) in one wrapper module: the cast is counted and named
declare enum Ealwaysthe enum (or a const object) where it is used
in declare module "pkg" in a script (the module declared)a function, class or a variable as aboveinstall the package's types, or a wrapper module that trusts what it imports
in declare module "pkg" in a module (an augmentation), declare global { interface Window } and other built-insa member added as always theremake it optional (appConfig?: Config), so the code reading it handles its absence
declare module "x"; with no bodyalways: every import from it is anydeclare module "*.css" {} for side-effect imports
declare field: T in a classT doesn't admit undefinedinitialize it, or field: T | undefined and check it (the same as field!: T, which no-unchecked-cast reports)
declare module "virtual:x" { const v: T; export default v }its value is typed anything but unknown: what a plugin generates is what nobody checksexport it as unknown and trust<T>(v) (or validate<T>) once, in a wrapper module
in declare module "./x" (the project's own module)a value (export function extra(): number)write it in ./x

Only counted: a wildcard module with a body (declare module "*.svg" { const url: string; export default url }: what the bundler makes of an asset), interfaces added to a module of the project (TypeScript checks the code that builds those objects), new types in declare global (a generated namespace Backend.Models), members added as optional, and a declaration in a module that nothing in it reads as a value (declare const brand: unique symbol, used only in types).

The project's own declaration files (env.d.ts, types.d.ts) are checked and counted with the files that use them: those TypeScript applies to the program, under the entries' directories. In a declaration file, a top-level value declaration is ambient without the declare keyword (export function run(): number;).

What the rule can't see ​

  • A bare read of a global declared Option<T> (harness rather than globalThis.harness) throws a ReferenceError when it was never set; the type says only that it may be missing.
  • Members a library's own namespace gets in declare global (namespace PixiMixins { interface Container { ... } }): the checker can't tell a library's namespace from new types, so these are counted, not reported, even when required.
  • An interface that adds members through extends (interface AllowedComponentProps extends ButtonHTMLAttributes {}): whether they are optional is in the other type.
  • Declarations in node_modules, and .d.ts files outside the entries' directories.

Related: no-unchecked-cast, the same assertion written with as. Guide: Escape hatches.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra