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

evolving-types不断发展的类型

Agent Skill

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

总安装

212

周安装

9

GitHub Stars

2

下载量

74
CodexClaudeCursorGemini CLI

安装说明

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

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

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

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

命令行安装

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

skills.shnpx skills
npx skills add https://github.com/marius-townhouse/effective-typescript-skills --skill evolving-types

简介

evolving-types 用于理解 TypeScript 中变量类型随赋值逐渐收窄的现象,辅助判断何时应显式标注类型。

  • 适用于处理初始值为 null/undefined、空数组或 any 类型的变量,帮助避免隐式类型脆弱性。
  • 强调 evolving types 虽可行但易出错,推荐优先采用显式注解以提高代码清晰度。
  • 使用时需结合实际赋值路径分析类型变化趋势,避免过度依赖自动推导导致维护风险。
  • 适用宿主包括 Codex、Claude、Cursor、Gemini CLI,接入前应确认版本、权限和运行环境要求。

SKILL.md

Understand Evolving Types

Overview

Some variables start with broad types and narrow as TypeScript sees values added.

This is an exception to the rule that types don't change. Variables initialized without a value, or as empty arrays, can have "evolving" types that narrow based on what you assign to them.

When to Use This Skill

  • Variables initialized to null or undefined
  • Arrays that start empty and get values pushed
  • Variables that start as any and narrow
  • Understanding when explicit annotations are better

The Iron Rule

Evolving types work but are fragile.
Prefer explicit annotations for clarity.

Remember:

  • Only applies to variables without initial typed values
  • Type evolves based on assignments
  • Final type is only valid after all assignments
  • Explicit annotation is often clearer

Detection: The Evolving any

const result = [];  // any[]
result.push('a');   // string[]
result.push(1);     // (string | number)[]

result
// ^? (string | number)[]

The type evolves with each push.

How Evolving Types Work

Uninitialized Variables

let val;  // any (evolving)
val
// ^? let val: any

if (Math.random() < 0.5) {
  val = /hello/;
  val
  // ^? let val: RegExp
} else {
  val = 12;
  val
  // ^? let val: number
}
val
// ^? let val: number | RegExp

TypeScript tracks assignments and computes the union.

Empty Arrays

const arr = [];  // any[] (evolving)
arr.push(1);
arr
// ^? number[]

arr.push('hello');
arr
// ^? (string | number)[]

null or undefined Initial Value

let x = null;  // any (evolving)
x
// ^? null

x = 12;
x
// ^? number

When Types Stop Evolving

Once a variable leaves its scope or is used in a function, its type is fixed:

function buildArray() {
  const arr = [];
  arr.push(1);
  arr.push(2);
  return arr;  // Type fixed as number[]
}

const myArray = buildArray();
myArray.push('hello');  // Error if return type is number[]

Problems with Evolving Types

Order Matters

const arr = [];
arr.push(1);
// arr is number[] here

// If this line is later:
arr.push('hello');
// arr is (string | number)[] but earlier uses assumed number[]

Implicit any

With noImplicitAny, empty arrays without annotation get any[]:

const values = [];  // Implicit any[] - may cause lint warnings

Fragile Inference

let x = null;
x = 'hello';
x = 42;  // Now it's string | number

// Later, someone adds:
x = true;  // Now it's string | number | boolean

// All code using x must handle all possibilities

Better: Explicit Annotations

// Clear intent, stable type
const result: number[] = [];
result.push(1);
result.push(2);
// result is always number[]

// Prevents accidents
result.push('hello');
//          ~~~~~~~
// Argument of type 'string' is not assignable to 'number'

When Evolving Types Are OK

Short, Simple Loops

const squares = [];
for (let i = 0; i < 5; i++) {
  squares.push(i * i);
}
// squares: number[] is clear from context

Accumulating Known Types

let result = null;
for (const item of items) {
  if (condition(item)) {
    result = item;
    break;
  }
}
// result evolves to ItemType | null

Functional Alternatives

Instead of evolving arrays, prefer functional constructs:

// Don't:
const doubled = [];
for (const n of numbers) {
  doubled.push(n * 2);
}

// Do:
const doubled = numbers.map(n => n * 2);
// ^? number[]

Type is inferred directly, no evolution needed.

Real-World Example

// Evolving (works but fragile)
async function fetchData() {
  let data;
  try {
    const response = await fetch('/api');
    data = await response.json();
  } catch (e) {
    data = null;
  }
  return data;  // any
}

// Better: explicit
async function fetchData(): Promise<Data | null> {
  try {
    const response = await fetch('/api');
    return await response.json();
  } catch (e) {
    return null;
  }
}

Pressure Resistance Protocol

1. "Evolving Types Work"

Pressure: "TypeScript figures it out automatically"

Response: It's fragile and can change unexpectedly with new code.

Action: Add explicit annotation for stability.

2. "I Don't Know the Type Yet"

Pressure: "The type depends on runtime conditions"

Response: You know the possible types; declare them.

Action: Use union type: let x: string | number | null = null;

Red Flags - STOP and Reconsider

  • const arr = [] without annotation
  • let x = null or let x without annotation
  • Types that change based on assignment order
  • any[] warnings in lint

Common Rationalizations (All Invalid)

ExcuseReality
"TypeScript infers it"It infers something, not necessarily what you want
"I'll add types later"Later never comes; add them now
"It's just temporary"Temporary code becomes permanent

Quick Reference

// EVOLVING (works but fragile)
const arr = [];       // any[]
arr.push(1);          // number[]
arr.push('a');        // (string | number)[]

// BETTER (stable and clear)
const arr: number[] = [];
arr.push(1);
arr.push('a');  // Error!

// BEST (functional)
const arr = items.map(item => item.value);
// Type inferred correctly

The Bottom Line

Evolving types are a convenience, not a best practice.

While TypeScript can track types through assignments, explicit annotations are clearer and more robust. Use evolving types only for simple, localized code. For anything else, declare your intent with annotations.

Reference

Based on "Effective TypeScript" by Dan Vanderkam, Item 25: Understand Evolving Types.

适合场景

01

用户想查找某类 Agent Skill 时

02

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

03

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

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

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

能力 4

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

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

平台分布

Codex

35.32%
按下载量换算26

Claude

30.24%
按下载量换算22

Cursor

18.73%
按下载量换算14

Gemini CLI

8.05%
按下载量换算6

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

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

安装前确认

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

来源信息

继续浏览同类 Skills