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

distinct-special-values独特的特殊价值观

Agent Skill

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

总安装

194

周安装

8

GitHub Stars

2

下载量

63
CodexClaudeCursorGemini CLI

安装说明

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

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

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

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

命令行安装

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

skills.shnpx skills
npx skills add https://github.com/marius-townhouse/effective-typescript-skills --skill distinct-special-values

简介

为特殊值定义独立类型的 TypeScript 最佳实践。

  • 避免使用 -1、空字符串等常规值表示异常状态。
  • 推荐使用 null、undefined 或标记联合类型区分场景。
  • 提升函数失败处理与缺失状态的类型安全性。
  • distinct-special-values 属于前端设计类 Skill,可作为该场景下的辅助能力补充。

SKILL.md

Use a Distinct Type for Special Values

Overview

Don't use -1, 0, or "" as special values. Use null or a distinct type.

When a function can fail or have a special case, represent it with a type that TypeScript can distinguish, not an in-domain value like -1 that's just a regular number.

When to Use This Skill

  • Functions that can fail (not found, error, etc.)
  • Values that have a "missing" or "unknown" state
  • Wrapping APIs that use sentinel values like -1
  • Designing your own return types

The Iron Rule

Special cases deserve distinct types.
Use null, undefined, or tagged unions - not -1 or "".

Remember:

  • -1 is just a number, indistinguishable from other numbers
  • TypeScript can't protect you from special values
  • null and undefined are trackable
  • Explicit error states are clearer than magic numbers

Detection: The -1 Trap

function splitAround<T>(vals: readonly T[], val: T): [T[], T[]] {
  const index = vals.indexOf(val);
  return [vals.slice(0, index), vals.slice(index + 1)];
}

splitAround([1, 2, 3, 4, 5], 6);
// Expected: error or [[1,2,3,4,5], []]
// Actual: [[1,2,3,4], [1,2,3,4,5]] (!)

Why? indexOf returns -1 for "not found", but -1 is a valid array index (counts from end).

Solution: Wrap with Distinct Type

function safeIndexOf<T>(vals: readonly T[], val: T): number | null {
  const index = vals.indexOf(val);
  return index === -1 ? null : index;
}

Now TypeScript forces you to handle both cases:

function splitAround<T>(vals: readonly T[], val: T): [T[], T[]] {
  const index = safeIndexOf(vals, val);
  return [vals.slice(0, index), vals.slice(index + 1)];
  //                   ~~~~~             ~~~~~
  // 'index' is possibly 'null'
}

Fixed version:

function splitAround<T>(vals: readonly T[], val: T): [T[], T[]] {
  const index = safeIndexOf(vals, val);
  if (index === null) {
    return [[...vals], []];
  }
  return [vals.slice(0, index), vals.slice(index + 1)];
}

Real-World Example: Product Price

// Bad: -1 means "unknown price"
interface Product {
  title: string;
  /** Price in dollars, or -1 if price is unknown */
  priceDollars: number;
}

// Disaster waiting to happen:
function getTotal(products: Product[]) {
  return products.reduce((sum, p) => sum + p.priceDollars, 0);
  // Whoops: products with unknown price make total negative!
}

Better:

interface Product {
  title: string;
  priceDollars: number | null;
}

function getTotal(products: Product[]) {
  return products.reduce((sum, p) => {
    if (p.priceDollars === null) {
      throw new Error(`Unknown price for ${p.title}`);
    }
    return sum + p.priceDollars;
  }, 0);
}

Why strictNullChecks Matters

Using -1 as a special value is like disabling strictNullChecks:

// @strictNullChecks: false
const truck: Product = {
  title: 'Tesla Cybertruck',
  priceDollars: null,  // ok with strictNullChecks off
};

When strictNullChecks is on, TypeScript distinguishes number from number | null. Using -1 as "unknown" bypasses this safety.

When to Use Tagged Unions

If null/undefined isn't clear enough, use a tagged union:

type RequestResult<T> =
  | { status: 'success'; data: T }
  | { status: 'error'; error: string }
  | { status: 'pending' };

function fetchUser(id: string): RequestResult<User> {
  // ...
}

const result = fetchUser('123');
if (result.status === 'success') {
  console.log(result.data.name);  // TypeScript knows data exists
}

Common Sentinel Values to Avoid

SentinelProblemAlternative
-1 (indexOf)Valid array indexnull
0Valid numbernull
""Valid stringnull
[]Valid arraynull
{}Valid objectnull
NaNnumber typenull or throw

Pressure Resistance Protocol

1. "JavaScript Uses -1"

Pressure: "indexOf returns -1, I should match that pattern"

Response: JavaScript's -1 is a historical mistake. Wrap it.

Action: Create wrapper returning T | null.

2. "It's Just a Placeholder"

Pressure: "We'll never actually use that value"

Response: Someone will forget. TypeScript won't protect you.

Action: Use a distinct type that TypeScript can track.

Red Flags - STOP and Reconsider

  • Magic numbers like -1, 0, or -999
  • Empty strings meaning "no value"
  • Comments explaining special values
  • Bugs from forgetting to check for special values

Common Rationalizations (All Invalid)

ExcuseReality
"It's a common pattern"Common doesn't mean good
"Performance is better"Marginal at best; safety matters more
"TypeScript can't track null"Yes it can, that's the point!

Quick Reference

// DON'T: Sentinel values
function indexOf(arr, val): number { ... }  // -1 means not found
function getPrice(): number { ... }  // -1 means unknown

// DO: Distinct types
function indexOf(arr, val): number | null { ... }
function getPrice(): number | null { ... }

// DO: Tagged unions for complex states
type Result<T> = { ok: true; value: T } | { ok: false; error: string };

The Bottom Line

Special cases deserve special types.

Using -1 or "" as special values bypasses TypeScript's type system. Use null, undefined, or tagged unions to represent special cases. TypeScript will then force you to handle them correctly.

Reference

Based on "Effective TypeScript" by Dan Vanderkam, Item 36: Use a Distinct Type for Special Values.

适合场景

01

用户想查找某类 Agent Skill 时

02

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

03

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

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

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

能力 4

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

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

平台分布

Codex

37.14%
按下载量换算23

Claude

27.65%
按下载量换算17

Cursor

20.9%
按下载量换算13

Gemini CLI

9.91%
按下载量换算6

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

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

安装前确认

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

来源信息

继续浏览同类 Skills