avoid-widening
Keep a type as narrow as the value it describes. An annotation wider than what inference already knows throws information away, so annotate only to add information. Keep immutable lookup tables literal with `as const satisfies`, type built keys with template literal types and build them `as const`, and widen on purpose only at a boundary where inference is unsound.
- License 许可
- MIT
- 所属仓库
- MetaMask/skills
- 最近更新
- 2026-11-14
- Stars
- ★ 25
- Forks
- 11
01 前言 Front Matter
- name
- avoid-widening
- maturity
- experimental
- description
- Keep a type as narrow as the value it describes. An annotation wider than what inference already knows throws information away, so annotate only to add information. Keep immutable lookup tables literal with `as const satisfies`, type built keys with template literal types and build them `as const`, and widen on purpose only at a boundary where inference is unsound.
02 资源文件 1
- 参考文档agents/openai.yaml543 B
03 SKILL.md 正文
Avoid Widening
Deepens MetaMask/contributor-docs docs/typescript.md, Avoid unintentionally widening an inferred type with a type annotation: "Enforcing a wider type defeats the purpose of adding an explicit type declaration, as it loses type information instead of adding it." derive-types covers where a type should come from. This skill covers how wide it should be, and avoid-any covers the annotation that turns checking off.
When To Use
- Writing or reviewing a type annotation, a
satisfiesclause or anas const. - A lookup table maps one set of names to another, such as trace names or action types.
- A collection is keyed by strings built from known parts, such as
`${name}:${id}`. - Proposing a type change in review, where a widening suggestion reads as a tightening.
Annotate to add information, never to repeat or lose it
Compare the annotation with the type inference already produces. Equal adds nothing, and wider loses information. Only a narrower annotation, or a deliberate widening at a boundary (below), is worth writing.
| declaration | resulting type |
|---|---|
const name = 'foo' |
"foo" |
const name: string = 'foo' |
string, widened |
const ids = [1, 2] as const |
readonly [1, 2] |
const ids: number[] = [1, 2] |
number[], widened |
To check a value against a type without widening it, use satisfies (docs/typescript.md L134-L136). The exception is a mutable object, array or class that code adds to later. There the narrowest type excludes the additions, so annotate with : unless the value is meant to be immutable (L201-L205).
Keep immutable lookup tables literal
// ? The annotation widens every value to `TraceName`, so `NAMES[name]` no longer says which member it is.
const NAMES: Record<FeatureTraceName, TraceName> = {
'Feature Open': TraceName.FeatureOpen,
'Feature Close': TraceName.FeatureClose,
};
// ✅ Checks every key and value, keeps each value's member type, and makes the table read-only.
const NAMES = {
'Feature Open': TraceName.FeatureOpen,
'Feature Close': TraceName.FeatureClose,
} as const satisfies Record<FeatureTraceName, TraceName>;
- A plain object literal with neither widens its values to
TraceName. satisfiesalone already keeps each value's member type when the checked type is a union of literals, as an enum is. Whatas constadds isreadonly, plus literal types for nested arrays and objects. It also marks the table as immutable, which is the condition under which the extensible-type exception above does not apply.
Type a built key by how it is built, and build it as const
// ✅ The map accepts only keys of this shape, so a key built from the wrong name does not compile.
const pending = new Map<`${FeatureTraceName}:${string}`, PendingSpan>();
const key = `${params.name}:${params.id}` as const;
pending.get(key);
- A template literal expression infers as
stringunless it is markedas constor contextually typed. So narrowing a collection's key type fails with TS2345 at eachget,setanddeletethat passes an unmarked key. Change the key type and every expression that builds a key together. - Measured with TypeScript 5.6.3 on metamask-extension#46123 (preload Perps markets on unlock) at
0118bcc256. Narrowing only the span map's key type produced five TS2345 errors, and addingas constto its two key expressions cleared them.
Widen on purpose only where inference is unsound, and only at a boundary
When a library's types claim more than its runtime guarantees, the correct annotation is wider. luxon types plus(duration) as returning this, so a valid DateTime<true> stays DateTime<true> after plus(), although the result is invalid past year 275760. Declaring the return as DateTimeMaybeValid restores the distinction the library erased.
That widening works only where the compiler has to use it:
| form | forces the check? |
|---|---|
const x: DateTimeMaybeValid = start.plus(d) |
no, because control-flow analysis re-narrows x to its initializer |
start.plus(d) satisfies DateTimeMaybeValid |
no, because it checks assignability and keeps the inferred type |
start.plus(d) as DateTimeMaybeValid |
yes |
a parameter or return typed DateTimeMaybeValid |
yes |
So the test is whether an annotation adds information inference lacks. Narrowing usually does. Widening does only where inference is wrong about the runtime.
Workflow
- Before writing
:, hover ortsc --noEmitto see the inferred type, and write the annotation only if it is narrower, or a boundary widening of an unsound type. - For a table that code never adds to, use
as const satisfies <Constraint>. For one that grows, annotate with:. - For a collection keyed by built strings, type the key as the template literal it is, and add
as constto every expression that builds one. Typecheck before proposing the change, since the key type and the builders have to change together. - In review, run the proposed suggestion through
tscbefore posting it. A narrowing that leaves the builders unmarked fails the build.
Related
derive-types— take a type from its authoritative source instead of restating it.avoid-any— the annotation that disables checking.tsc-blindspots— divergence shapes the compiler cannot report, including a widened local.
04 同仓库更多 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.
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.
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.
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
smart-accounts-kit
Build dApps with MetaMask Smart Accounts Kit — ERC-4337 smart accounts, delegations, and Advanced Permissions (ERC-7715)
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.