Token导航 LogoToken导航TokenDH.com
前端设计只读github未标认证来源可访问许可证需确认审计通过

consistent-aliases一致的别名

Agent Skill

consistent-aliases 用于处理 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:consistent-aliases(一致的别名)
来源仓库:https://github.com/marius-townhouse/effective-typescript-skills
仓库路径:skills/consistent-aliases
安装命令:
npx skills add https://github.com/marius-townhouse/effective-typescript-skills --skill consistent-aliases
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

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

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

简介

一致的别名技能帮助 TypeScript 开发者规范变量与对象属性的引用方式,避免类型收窄失效。

  • 适用于需要严格类型安全的前端工程,特别是涉及联合类型和条件渲染的场景。
  • 通过 GitHub 安装并使用 npx skills add 命令添加,提供最佳实践指导和常见陷阱提醒。
  • 推荐在大型项目中启用,配合 ESLint 规则使用效果更佳。
  • consistent-aliases 属于前端设计类 Skill,可作为该场景下的辅助能力补充。

SKILL.md

Be Consistent in Your Use of Aliases

Overview

If you create an alias, use it consistently.

When you assign an object property to a variable, you create an alias. TypeScript tracks them separately, so narrowing one doesn't narrow the other. Choose one and stick with it.

When to Use This Skill

  • Type narrowing isn't working as expected
  • Using both a variable and its source property
  • Property checks not affecting aliased variables
  • Refinements being "lost" after function calls

The Iron Rule

One alias OR the original - never mix them in the same scope.
Prefer destructuring for consistent naming.

Remember:

  • Aliases and originals are tracked separately
  • Narrowing one doesn't narrow the other
  • Function calls can invalidate property refinements
  • Local variables are safer than object properties

Detection: Alias Breaks Narrowing

interface Polygon {
  exterior: Coordinate[];
  bbox?: BoundingBox;
}

function isPointInPolygon(polygon: Polygon, pt: Coordinate) {
  const box = polygon.bbox;  // Created an alias

  if (polygon.bbox) {  // Narrows polygon.bbox
    if (pt.x < box.x[0]) {  // box is still possibly undefined!
      //       ~~~
      // 'box' is possibly 'undefined'
    }
  }
}

The check on polygon.bbox doesn't narrow box.

The Golden Rule: Use Aliases Consistently

function isPointInPolygon(polygon: Polygon, pt: Coordinate) {
  const box = polygon.bbox;

  if (box) {  // Check the alias
    if (pt.x < box.x[0]) {  // Use the alias
      // OK - box is narrowed here
    }
  }
}

Now both the check and usage refer to the same variable.

Best Practice: Use Destructuring

function isPointInPolygon(polygon: Polygon, pt: Coordinate) {
  const { bbox } = polygon;  // Same name as property

  if (bbox) {
    const { x, y } = bbox;  // Continue destructuring
    if (pt.x < x[0] || pt.x > x[1] || pt.y < y[0] || pt.y > y[1]) {
      return false;
    }
  }
  return true;
}

Benefits:

  • Consistent naming (no box vs bbox confusion)
  • More concise
  • Works well with TypeScript's control flow

Aliases at Runtime Too

Aliasing affects runtime behavior:

const { bbox } = polygon;

if (!bbox) {
  calculatePolygonBbox(polygon);  // Fills in polygon.bbox
  // Now polygon.bbox exists, but bbox is still undefined!
}

The alias and original can diverge at runtime.

Function Calls and Refinements

TypeScript makes a pragmatic choice about function calls:

function expandPolygon(p: Polygon) { /* ... */ }

if (polygon.bbox) {
  polygon.bbox  // BoundingBox (narrowed)

  expandPolygon(polygon);

  polygon.bbox  // Still BoundingBox (TypeScript trusts you)
  // But the function might have set it to undefined!
}

TypeScript assumes functions don't invalidate refinements. This is usually fine but can be wrong.

Safer: Local Variables

if (polygon.bbox) {
  const bbox = polygon.bbox;  // Capture it locally

  expandPolygon(polygon);

  bbox  // Still BoundingBox - local variable is safe
}

Local variables can't be changed by function calls.

Readonly for Extra Safety

function safeExpand(p: Readonly<Polygon>) {
  // Can't modify p.bbox
}

if (polygon.bbox) {
  safeExpand(polygon);
  polygon.bbox  // Guaranteed still BoundingBox
}

Readonly parameters prevent mutation concerns.

Common Patterns

Nullish Coalescing Instead of Alias

// Instead of:
const name = person.nickname;
const displayName = name ? name : person.fullName;

// Use:
const displayName = person.nickname ?? person.fullName;

Map.get Pattern

// TypeScript doesn't connect has() and get():
if (map.has(key)) {
  const value = map.get(key);  // Still T | undefined
}

// Better pattern:
const value = map.get(key);
if (value !== undefined) {
  value  // T (narrowed)
}

// Or with nullish coalescing:
const value = map.get(key) ?? defaultValue;

Pressure Resistance Protocol

1. "I Need Both Names"

Pressure: "Sometimes I use the property, sometimes the variable"

Response: Pick one. Mixing them breaks narrowing.

Action: Use destructuring for consistent naming.

2. "The Function Won't Modify It"

Pressure: "I know expandPolygon doesn't touch bbox"

Response: TypeScript can't know that. Document with Readonly.

Action: Use local variables or readonly parameters for safety.

Red Flags - STOP and Reconsider

  • Variable created from property, then property used in condition
  • Type errors about "possibly undefined" after an if check
  • Mixing property access and variable use in the same block
  • Relying on refinements after function calls

Common Rationalizations (All Invalid)

ExcuseReality
"The alias is the same thing"TypeScript tracks them separately
"I already checked the property"Check doesn't narrow the alias
"Functions won't mutate it"TypeScript can't verify that

Quick Reference

// DON'T: Mix alias and original
const box = polygon.bbox;
if (polygon.bbox) {  // Checks original
  box.x;  // Uses alias - still possibly undefined!
}

// DO: Use alias consistently
const box = polygon.bbox;
if (box) {  // Checks alias
  box.x;  // Uses alias - narrowed correctly
}

// DO: Use destructuring
const { bbox } = polygon;
if (bbox) {
  const { x, y } = bbox;
}

The Bottom Line

Choose one name and use it consistently.

When you alias a property, TypeScript tracks the alias and original separately. Narrowing one doesn't narrow the other. Use destructuring for consistent naming, and prefer local variables when function calls might invalidate refinements.

Reference

Based on "Effective TypeScript" by Dan Vanderkam, Item 23: Be Consistent in Your Use of Aliases.

适合场景

01

用户想查找某类 Agent Skill 时

02

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

03

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

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

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

能力 4

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

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

平台分布

Codex

36.92%
按下载量换算23

Claude

29.33%
按下载量换算18

Cursor

19.6%
按下载量换算12

Gemini CLI

8.71%
按下载量换算5

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

只读

该 Skill 主要提供规则、说明或参考内容,本身偏只读;真正读写文件、联网或执行命令仍取决于宿主 Agent 的任务。

安装前确认

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

来源信息

继续浏览同类 Skills