Token导航 LogoToken导航TokenDH.com
开发需要联网github未标认证来源可访问许可证需确认审计通过

understand-type-widening了解类型扩展

Agent Skill

understand-type-widening 用于处理 GitHub 仓库、Issue、Pull Request 和代码协作信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要围绕仓库状态、代码变更或协作事项进行整理时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

总安装

196

周安装

8

GitHub Stars

2

下载量

63
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:understand-type-widening(了解类型扩展)
来源仓库:https://github.com/marius-townhouse/effective-typescript-skills
仓库路径:skills/understand-type-widening
安装命令:
npx skills add https://github.com/marius-townhouse/effective-typescript-skills --skill understand-type-widening
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/marius-townhouse/effective-typescript-skills --skill understand-type-widening

简介

用于处理 GitHub 仓库、Issue、Pull Request 和代码协作信息,适合在 Codex、Claude、Cursor、Gemini CLI 中整理项目状态。

  • 支持围绕仓库变更、协作事项进行信息梳理与分析,提升开发协同效率。
  • 通过 npx skills add 命令从指定 GitHub 仓库安装并使用。
  • 安装前需确认权限范围、维护状态及是否涉及联网或文件操作。
  • understand-type-widening 属于开发类 Skill,可作为该场景下的辅助能力补充。

SKILL.md

Understand Type Widening

Overview

When TypeScript infers a type from a value, it often widens it.

A variable initialized with "x" could be intended to hold any string, or just the literal "x". TypeScript guesses using heuristics, and understanding these helps you write predictable code.

When to Use This Skill

  • Confused why a type is string instead of "specific-value"
  • const and let give different types for the same value
  • Array literals get unexpected element types
  • Object properties are wider than expected
  • Type errors about literals not being assignable

The Iron Rule

ALWAYS understand how your declaration style affects inferred types.

Remember:

  • let variables widen literals to their base type
  • const variables keep literal types (for primitives)
  • Object/array contents widen even with const
  • Use as const for full literal inference

Detection: The Widening Surprise

If TypeScript infers a broader type than you expected, you're seeing widening.

// Primitive widening with let
let x = 'x';
//  ^? let x: string  (not "x")

// No widening with const (for primitives)
const y = 'y';
//    ^? const y: "y"

// But object properties still widen
const obj = { x: 1, y: 2 };
//    ^? const obj: { x: number; y: number }  (not { x: 1, y: 2 })

Why Widening Exists

TypeScript must balance two goals:

  1. Specificity - Catch real bugs with narrow types
  2. Flexibility - Allow reasonable mutations
// Without widening, this would fail:
let x = 'x';  // If inferred as "x", then...
x = 'y';      // Error! "y" is not assignable to "x"

// With widening:
let x = 'x';  // Inferred as string
x = 'y';      // OK

The Widening Rules

Rule 1: let Widens, const Preserves (Primitives)

// let → widened
let a = 'hello';
//  ^? let a: string

let b = 42;
//  ^? let b: number

let c = true;
//  ^? let c: boolean

// const → literal
const d = 'hello';
//    ^? const d: "hello"

const e = 42;
//    ^? const e: 42

const f = true;
//    ^? const f: true

Rule 2: Object Properties Always Widen

const point = { x: 3, y: 4 };
//    ^? const point: { x: number; y: number }

// Not { x: 3, y: 4 } - because you might do:
point.x = 10;  // This must be valid

Rule 3: Array Elements Widen

const arr = [1, 2, 3];
//    ^? const arr: number[]

// Not [1, 2, 3] or readonly [1, 2, 3]

Rule 4: Mixed Arrays Get Union Types

const mixed = [1, 'x'];
//    ^? const mixed: (string | number)[]

// TypeScript picks the "best common type"

Controlling Widening

Use as const for Full Literal Inference

// Regular object - properties widen
const obj1 = { x: 1, y: 2 };
//    ^? const obj1: { x: number; y: number }

// With as const - properties are literal and readonly
const obj2 = { x: 1, y: 2 } as const;
//    ^? const obj2: { readonly x: 1; readonly y: 2 }

// Arrays too
const arr = [1, 2, 3] as const;
//    ^? const arr: readonly [1, 2, 3]

Use Type Annotations to Be Explicit

// Annotate to get the type you want
const x: 'x' | 'y' = 'x';
//    ^? const x: "x" | "y"

// Can be reassigned to 'y', but nothing else

Use Satisfies for Checked Inference

type Point = { x: number; y: number };

// Type annotation: loses literal types
const p1: Point = { x: 1, y: 2 };
//    ^? const p1: Point

// satisfies: keeps literals while checking structure
const p2 = { x: 1, y: 2 } satisfies Point;
//    ^? const p2: { x: number; y: number }

p2.x;  // number (not 1, but still good for inference)

Common Widening Problems

Problem: Literal Expected, Got String

type HTTPMethod = 'GET' | 'POST' | 'PUT';

function makeRequest(method: HTTPMethod) { /* ... */ }

let method = 'GET';
makeRequest(method);
//          ~~~~~~ Argument of type 'string' is not assignable

Solutions:

// Solution 1: Use const
const method = 'GET';
makeRequest(method);  // OK

// Solution 2: Type annotation
let method: HTTPMethod = 'GET';
makeRequest(method);  // OK

// Solution 3: as const
let method = 'GET' as const;
makeRequest(method);  // OK

Problem: Object Property Too Wide

type Config = {
  mode: 'development' | 'production';
  debug: boolean;
};

function configure(config: Config) { /* ... */ }

const config = { mode: 'development', debug: true };
configure(config);
//        ~~~~~~ Type 'string' is not assignable to type '"development" | "production"'

Solutions:

// Solution 1: Type annotation
const config: Config = { mode: 'development', debug: true };

// Solution 2: as const on the whole object
const config = { mode: 'development', debug: true } as const;

// Solution 3: as const on just the property
const config = { mode: 'development' as const, debug: true };

Problem: Tuple Becomes Array

function setPoint(point: [number, number]) { /* ... */ }

const coords = [10, 20];
setPoint(coords);
//       ~~~~~~ Type 'number[]' is not assignable to type '[number, number]'

Solutions:

// Solution 1: Type annotation
const coords: [number, number] = [10, 20];

// Solution 2: as const (makes it readonly)
const coords = [10, 20] as const;
// Note: readonly [10, 20] may not be assignable to [number, number]
// depending on the function's type

Pressure Resistance Protocol

1. "Just Use any"

Pressure: "Type is wrong, just cast it to any"

Response: The type is right, just wider than you want.

Action: Use const, type annotations, or as const.

2. "TypeScript Is Being Dumb"

Pressure: "It's obviously 'GET', why infer string?"

Response: TypeScript assumes let variables will change.

Action: Use const for values that won't change.

Red Flags - STOP and Reconsider

  • Casting to fix literal type errors
  • Surprised that object properties are string not "specific"
  • Tuple types becoming arrays
  • Union literals widening unexpectedly

Common Rationalizations (All Invalid)

ExcuseReality
"const should make it literal"Only for primitives, not object contents
"The value is clearly X"let means it could change
"as any fixes it"Use proper narrowing controls

Quick Reference

DeclarationValueInferred Type
let x = "hello"string literalstring
const x = "hello"string literal"hello"
const x = {a: 1}object{a: number}
const x = {a: 1} as constobject{readonly a: 1}
const x = [1, 2]arraynumber[]
const x = [1, 2] as constarrayreadonly [1, 2]

The Bottom Line

TypeScript widens types to allow reasonable mutations.

Use const for primitives, type annotations for explicit types, and as const when you need full literal inference. Understanding widening prevents surprising type errors and helps you write more precise types.

Reference

Based on "Effective TypeScript" by Dan Vanderkam, Item 20: Understand How Variables Get Their Types.

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

37.31%
按下载量换算24

Claude

31.32%
按下载量换算20

Cursor

19.02%
按下载量换算12

Gemini CLI

9.6%
按下载量换算6

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills