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

exclusive-or-properties专有或属性

Agent Skill

exclusive-or-properties 用于查找、检索和筛选相关信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要根据关键词、任务场景或来源线索快速定位候选结果时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

总安装

233

周安装

10

GitHub Stars

2

下载量

82
CodexClaudeCursorGemini CLI

安装说明

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

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

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

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

命令行安装

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

skills.shnpx skills
npx skills add https://github.com/marius-townhouse/effective-typescript-skills --skill exclusive-or-properties

简介

exclusive-or-properties 使用 TypeScript 可选属性建模互斥配置选项。

  • 适用于组件 props 或 API 参数中仅允许一个字段生效的场景。
  • 比联合类型更安全,比运行时检查更清晰表达意图。
  • 通过 GitHub 安装,需启用 strictNullChecks 编译选项。
  • 推荐用于表单控件、开关组和条件渲染逻辑。

SKILL.md

Use Optional Never Properties to Model Exclusive Or

Overview

Sometimes you need a type where exactly one of several properties must be present, but not more than one. This "exclusive or" (XOR) pattern is common in component props, API parameters, and configuration objects. Using optional properties with never types enforces this constraint at compile time.

This technique provides better type safety than unions of objects and clearer intent than runtime checks.

When to Use This Skill

  • Exactly one of several properties must be present
  • Modeling mutually exclusive configuration options
  • Component props with alternative configurations
  • API parameters that have exclusive variants
  • Preventing invalid combinations of properties

The Iron Rule

Use optional never properties to enforce "exactly one of" constraints. Each variant makes its property required and others never.

Detection

Watch for these invalid combinations:

// RED FLAGS - Invalid combinations allowed
interface Config {
  url?: string;
  filePath?: string;
  content?: string;
}
// Can pass none, one, or all - too permissive!

// Runtime validation needed:
if ((config.url ? 1 : 0) + (config.filePath ? 1 : 0) + (config.content ? 1 : 0) !== 1) {
  throw new Error('Exactly one source required');
}

The Problem

interface LoadConfig {
  url?: string;      // Load from URL
  filePath?: string; // Load from file
  content?: string;  // Load from string
}

// All these are allowed, but shouldn't be:
const bad1: LoadConfig = {};  // No source specified
const bad2: LoadConfig = { url: '...', filePath: '...' };  // Two sources
const bad3: LoadConfig = { url: '...', filePath: '...', content: '...' };  // All three

// Runtime check required
function load(config: LoadConfig) {
  const sources = [config.url, config.filePath, config.content].filter(Boolean);
  if (sources.length !== 1) {
    throw new Error('Exactly one source required');
  }
  // ...
}

The Solution: Optional Never Properties

type LoadConfig =
  | { url: string; filePath?: never; content?: never }
  | { url?: never; filePath: string; content?: never }
  | { url?: never; filePath?: never; content: string };

// Valid - exactly one source:
const good1: LoadConfig = { url: 'https://example.com' };
const good2: LoadConfig = { filePath: '/path/to/file' };
const good3: LoadConfig = { content: 'raw content' };

// Invalid - caught at compile time:
const bad1: LoadConfig = {};  // Error: missing required property
const bad2: LoadConfig = { url: '...', filePath: '...' };  // Error: filePath must be undefined
const bad3: LoadConfig = { url: '...', content: '...' };   // Error: content must be undefined

Component Props Example

type ButtonProps = {
  label: string;
  onClick: () => void;
} & (
  | { href: string; to?: never; onPress?: never }      // External link
  | { href?: never; to: string; onPress?: never }     // Router link
  | { href?: never; to?: never; onPress: () => void } // Custom action
);

// Usage
<Button label="External" href="https://example.com" />
<Button label="Internal" to="/dashboard" />
<Button label="Action" onPress={handlePress} />

// Error: can't combine variants
<Button label="Bad" href="..." to="..." />  // Error!

API Parameters Example

type SearchParams = {
  limit?: number;
  offset?: number;
} & (
  | { query: string; filters?: never }
  | { query?: never; filters: Filter[] }
);

// Valid
search({ query: 'typescript' });
search({ filters: [{ field: 'status', value: 'active' }] });
search({ query: 'typescript', limit: 10 });

// Invalid
search({});  // Error: need query or filters
search({ query: '...', filters: [...] });  // Error: can't have both

Generic XOR Helper

type XOR<A, B> =
  | (A & { [K in keyof B]?: never })
  | (B & { [K in keyof A]?: never });

// Usage
type Config = XOR<
  { url: string },
  XOR<
    { filePath: string },
    { content: string }
  >
>;

// Or for exactly one of many:
type ExactlyOne<T> = {
  [K in keyof T]: { [P in K]: T[K] } & {
    [P in Exclude<keyof T, K>]?: never;
  };
}[keyof T];

type LoadConfig2 = ExactlyOne<{
  url: string;
  filePath: string;
  content: string;
}>;

Pressure Resistance Protocol

When enforcing exclusive properties:

  1. Identify exclusivity: Which properties are mutually exclusive?
  2. Create variants: Each variant has one required, others never
  3. Union the variants: Combine with | operator
  4. Test combinations: Ensure invalid combos fail
  5. Document the pattern: Explain why certain combos are invalid

Red Flags

Anti-PatternProblemSolution
All optional propertiesAllows none or manyOptional never pattern
Runtime validation onlyErrors at runtime, not compile timeType-level enforcement
Comments saying "use only one"Not enforcedMake it a type error

Common Rationalizations

"I'll validate at runtime"

Reality: Runtime validation catches bugs later. Type-level enforcement catches them immediately.

"This is too verbose"

Reality: Use helper types like XOR or ExactlyOne to reduce repetition.

"Users might want multiple options"

Reality: If that's valid, don't use XOR. If it's not, enforce it at the type level.

Quick Reference

PatternSyntaxMeaning
XOR (2 props)`{a: T, b?: never} \{a?: never, b: T}`Exactly one
XOR (3+ props)Union of variantsExactly one
Helper typeXOR<A, B>Reusable pattern

The Bottom Line

Use optional never properties to enforce "exactly one of" constraints at compile time. This eliminates an entire class of runtime errors and makes invalid states unrepresentable.

Reference

  • Effective TypeScript, 2nd Edition by Dan Vanderkam
  • Item 63: Use Optional Never Properties to Model Exclusive Or

适合场景

01

用户想查找某类 Agent Skill 时

02

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

03

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

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

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

能力 4

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

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

平台分布

Codex

35.7%
按下载量换算29

Claude

29.11%
按下载量换算24

Cursor

20.42%
按下载量换算17

Gemini CLI

9.01%
按下载量换算7

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

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

安装前确认

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

来源信息

继续浏览同类 Skills