AGENT SKILL

avoid-any

Handle `any` correctly — it is not a type but a directive that disables type checking. Substitute by position (assignee → `unknown`, assigned → `never`). Two narrow exceptions: a generic constraint, and a callback parameter caught in a bivariant position between two fixed, irresolvable function-type constraints — both declared with an inline eslint-disable. Also covers the `any` that is never written down: a precise signature fed `any` at every call site, which no lint rule and no compiler check can report.

License 许可
MIT
所属仓库
MetaMask/skills
最近更新
2026-11-14
Stars
★ 25
Forks
11

01 前言 Front Matter

name
avoid-any
maturity
experimental
description
Handle `any` correctly — it is not a type but a directive that disables type checking. Substitute by position (assignee → `unknown`, assigned → `never`). Two narrow exceptions: a generic constraint, and a callback parameter caught in a bivariant position between two fixed, irresolvable function-type constraints — both declared with an inline eslint-disable. Also covers the `any` that is never written down: a precise signature fed `any` at every call site, which no lint rule and no compiler check can report.

02 资源文件 4

03 SKILL.md 正文

Avoid any

Deepens the TypeScript guidance in mms-coding-guidelines — which already says "avoid any, prefer unknown" and links MetaMask/contributor-docs docs/typescript.md. This is the reasoning layer beneath that bullet: why any is dangerous, and how to replace it by position. Grounded in docs/typescript.md (§ Avoid any).

any is not a type — it is a directive that disables type checking

The mental model matters more than the ESLint rule, because @typescript-eslint/no-explicit-any (already error in extension CI) does not stop the reasoning that reaches for any:

  • any is not "the widest type" — that is unknown. any is a compiler directive that disables type checking for the value it annotates.
  • It suppresses every error about its assignee — the equivalent of @ts-ignore on every use of that variable. The errors still affect the code; any only makes them invisible.
  • It subsumes what it touches. Any type in a union, intersection, or property relationship with any becomes any — an unmitigated loss of type information.
  • It infects downstream code. One any at a source (e.g. a library type that resolves to any) propagates silently through every consumer, converting compile-time errors into silent runtime failures — defeating the point of a statically-typed language.

Substitute any by position — assignee vs assigned

Identify which side of an assignment the any sits on:

  • Assignee (a variable, parameter, or return that receives a value — "it could be anything"): try unknown first, then narrow. unknown is the true universal supertype: everything is assignable to it, but it forces a type guard before use. any ↔ unknown are interchangeable in this position, so it is almost always a safe swap.
    • ? type Fn = () => any; const xs: any[]
    • ✅ type Fn = () => unknown; const xs: unknown[]
  • Assigned (a value that flows into a slot): try never first, then widen to a subtype of the assignee's type. unknown cannot substitute here (it is only assignable to unknown); never is the bottom type, assignable to everything.

Acceptable exception 1 — generic constraints

any is acceptable in a generic constraint, and only there. It bounds a type parameter without being assigned to a value, so it neither pollutes nor infects.

class BaseController<
  ControllerName extends string,
  ControllerState extends StateConstraint,
  ControllerMessenger extends Messenger<
    ControllerName,
    ActionConstraint,
    EventConstraint,
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    any
  >,
> // ...

Three conditions attach:

  • Declare it explicitly. no-explicit-any is error, so a constraint any needs an inline // eslint-disable-next-line @typescript-eslint/no-explicit-any at the site — a deliberate, visible exception.
  • Constraints only — never a generic argument. Passing any as an argument (ControllerMessenger<any, any>) is ? — that assigns any and infects. Constraint-vs-argument is the whole distinction.
  • Prefer a narrower constraint anyway. Reach for any here only when the narrower bound is genuinely unavailable.

Acceptable exception 2 — a callback parameter between two irresolvable function-type constraints

A callback's parameter may be any when all three hold:

  1. Bivariant position — the callback is assignable to a wider function type and an assignee of a narrower one.
  2. Irresolvable — no concrete type satisfies both directions, because the wider function type is not a supertype of the narrower (WideParam extends T extends NarrowParam has no solution).
  3. Fixed — neither constraint can be redesigned without breaking callers or losing accuracy.

Under --strictFunctionTypes parameters are contravariant, so the callback's parameter must be a supertype of the outer slot's (outward: unknown ✓, never ✗) and a subtype of the incoming value's (inward: never ✓, unknown ✗). any is the only inhabitant of both the top and bottom of the assignability lattice, so it is the only escape. (Return types stay covariant — keep unknown.)

? If the constraints are redesignable, the contravariance error is a real design smell — fix it (usually by parametrizing with a generic), don't suppress:

declare const acceptGeneric: <E>(handler: (event: E) => void) => void;
acceptGeneric(onA); // no `any` needed

✅ Only when the constraints are fixed and irresolvable:

// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Bivariant position with irresolvable, fixed constraints
let bridge: (x: any) => void;

Like the generic-constraint case, this any is not infectious — it is scoped to one parameter position, and both constraint types re-impose their signatures at each use site. Annotate the eslint-disable with the criteria so a reviewer can check them. Caveat: the safety claim holds only if the constraint types are accurate — a fixed constraint that is itself imprecise (a library type that is any internally) still forces the bridge any but no longer preserves safety at the use sites.

Canonical instance: a messenger registerActionHandler slot typed (...args: any[]) => any — strongly-typed handlers flow inward at registration, strongly-typed argument tuples outward at dispatch; unknown[] fails registration, never[] fails dispatch. It encodes rank-N polymorphism (∀α. (α) => R) that TypeScript cannot express directly.

When any still seems unavoidable, prefer the narrower, greppable escape hatches: as unknown as as a documented last resort, or @ts-expect-error with a comment saying why (a TODO where a fix is planned), never @ts-ignore. An unneeded @ts-expect-error fails tsc with Unused '@ts-expect-error' directive while an unneeded @ts-ignore stays silent, and @typescript-eslint/ban-ts-comment bans @ts-ignore and requires a description on @ts-expect-error. To replace a member, assert the replacement instead of erasing the target: target.method = stub as typeof target.method, not (target as any).method = stub, so tsc still rejects a stub whose type does not overlap the original's. Never reach for any to unblock feature work "to fix later."

Declared any beats absorbed any — and only one of them is countable

The two exceptions above are both declared: the any is written down, an inline disable sits next to it, and a reviewer can find it with grep. The dangerous case is the one where no any appears anywhere and the value is any regardless:

  • ? Absorbed. A precise annotation on a parameter or return that receives any at every call site — hexValueIsEmpty(value: string | null | undefined) fed ethers dynamic-method results. no-explicit-any never fires, because no any was written. tsc never fires, because any satisfies every annotation. The file reads as checked and none of it is.
  • ✅ Declared. ): Promise<any> with an inline disable and a linked issue, as shared/lib/token-util.ts does for the same ethers API. Nothing is safer at runtime, but the claim is now honest, greppable, and countable by CI.

The absorbed form is strictly worse than the declared one, and it is what a JS→TS conversion produces by default: the writer annotates what the value ought to be, and any accepts the annotation without comment.

A conversion is where the boundary's type is chosen, so an absorbed any is a decision, not an inheritance. With checkJs off the predecessor asserted nothing; the precise signature is new. "The any is pre-existing" is true of the library and false of the annotation next to it.

Detect it with IsAny<T> at the call sites rather than by reading the signature — the probe, its controls, and the declare module composition that turns an any into a confident string are in the tsc-blindspots skill. The fix is to type or validate the value where it enters, so the precise annotations downstream are earned.

04 同仓库更多 Skills

MetaMask
MetaMask/skills

recipe-plan-and-dispatch

Plan a ticket with the engineer before a recipe fix-bug or dev run. Agree the acceptance criteria and an implementation plan, write it as an approved plan file, and print the prompt that starts the run with any agent and model. Never edits product code or starts the run.

★ 25
MetaMask
MetaMask/skills

recipe-library-audit

Run every recipe of a team recipe library on one client with mm-harness, classify each result with the failing node and exact error, and review naming and composability. Produces a per-client table; changes nothing.

★ 25
MetaMask
MetaMask/skills

recipe-performance

Diagnose a specific MetaMask user flow's real-app performance and Sentry critical-user-flow telemetry with deterministic mm-harness recipes. Use for CUF boundaries, cold/warm/background-resume behavior, latency regressions, lifecycle tags, duplicate or blocking network requests, long tasks, layout/paint cost, screen redraws, React rerenders, or browser trace analysis where tests alone cannot prove the real app.

★ 25
MetaMask
MetaMask/skills

x402-payments

Build x402 payment flows using MetaMask Smart Accounts Kit, machine to machine payments over HTTP with ERC-7710 delegations and ERC-7715 Advanced Permissions

★ 25
MetaMask
MetaMask/skills

smart-accounts-kit

Build dApps with MetaMask Smart Accounts Kit — ERC-4337 smart accounts, delegations, and Advanced Permissions (ERC-7715)

★ 25
MetaMask
MetaMask/skills

oh-my-opencode

Multi-agent orchestration plugin for OpenCode. Use when the user wants to install, configure, or operate oh-my-opencode — including agent delegation, ultrawork mode, Prometheus planning, background tasks, category-based task routing, model resolution, tmux integration, or any oh-my-opencode feature. Covers installation, configuration, all agents (Sisyphus, Oracle, Librarian, Explore, Atlas, Prometheus, Metis, Momus), all categories, slash commands, hooks, skills, MCPs, and troubleshooting.

★ 25