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

limit-optional-properties限制可选属性

Agent Skill

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

总安装

218

周安装

9

GitHub Stars

2

下载量

71
CodexClaudeCursorGemini CLI

安装说明

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

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

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

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

命令行安装

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

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

简介

limit-optional-properties 用于处理 GitHub 仓库、Issue、Pull Request 和代码协作信息,适合围绕仓库状态和代码变更进行整理。

  • 适用于代码协作和项目维护场景,可在 Codex、Claude、Cursor、Gemini CLI 中使用。
  • 通过 npx skills add 命令从指定 GitHub 仓库安装,需确认权限范围和维护状态。
  • 安装前建议检查是否会触发联网、命令执行或文件读写等操作。
  • 适用宿主包括 Codex、Claude、Cursor、Gemini CLI,接入前应确认版本、权限和运行环境要求。

SKILL.md

Limit the Use of Optional Properties

Overview

Optional properties are convenient but costly.

Every optional property creates uncertainty. Readers must check if it exists. Code paths multiply. Consider whether required properties or separate types are better.

When to Use This Skill

  • Adding new properties to existing types
  • Designing interfaces with optional fields
  • Migrating types from JavaScript
  • Choosing between optional and required

The Iron Rule

Required properties are simpler to work with.
Use optional properties only when absence is meaningful.

Remember:

  • Optional = uncertainty in every usage
  • Type narrowing is required for optional properties
  • Multiple optionals = exponential complexity
  • Consider: is absence a valid state?

Detection: Optional Overload

interface FormattedValue {
  value: number;
  units: string;
  unitSystem?: 'metric' | 'imperial';  // New optional property
}

function formatValue(val: FormattedValue): string {
  // Now EVERY usage must consider: is unitSystem set?
  if (val.unitSystem === 'metric') {
    // ...
  } else if (val.unitSystem === 'imperial') {
    // ...
  } else {
    // undefined case - what does it mean?
  }
}

Combinatorial Explosion

With n optional properties, there are 2^n possible states:

interface Config {
  host?: string;      // 2 states
  port?: number;      // x2 = 4 states
  timeout?: number;   // x2 = 8 states
  retries?: number;   // x2 = 16 states
}

Many combinations may be invalid!

Alternative 1: Required with Defaults

// Instead of optional properties
interface Config {
  host: string;
  port: number;
  timeout: number;
}

// Provide defaults at construction
function createConfig(overrides: Partial<Config>): Config {
  return {
    host: 'localhost',
    port: 8080,
    timeout: 5000,
    ...overrides
  };
}

Now Config always has all properties. Simpler to use!

Alternative 2: Separate Types

// Instead of one type with optionals
interface BasicFormattedValue {
  value: number;
  units: string;
}

interface LocalizedFormattedValue {
  value: number;
  units: string;
  unitSystem: 'metric' | 'imperial';
}

type FormattedValue = BasicFormattedValue | LocalizedFormattedValue;

Now the relationship between properties is explicit.

Alternative 3: Tagged Union

type FormattedValue =
  | { type: 'basic'; value: number; units: string }
  | { type: 'localized'; value: number; units: string; unitSystem: 'metric' | 'imperial' };

function format(val: FormattedValue): string {
  switch (val.type) {
    case 'basic':
      return `${val.value} ${val.units}`;
    case 'localized':
      // val.unitSystem is guaranteed to exist here
      return formatLocalized(val);
  }
}

When Optional IS Appropriate

Truly Independent Options

interface RequestOptions {
  timeout?: number;   // Default behavior is fine
  headers?: Headers;  // No headers is valid
  cache?: boolean;    // Default is acceptable
}

These options are independently meaningful and have sensible defaults.

Backward Compatibility

// v1
interface User {
  name: string;
}

// v2 - adding optional to avoid breaking changes
interface User {
  name: string;
  email?: string;  // Old code still works
}

But consider: should you version the type instead?

Group Related Optionals

If properties are related, group them:

// Bad: related properties are separately optional
interface Person {
  name: string;
  birthPlace?: string;  // If one is set...
  birthDate?: Date;     // ...the other probably should be too
}

// Good: grouped together
interface Person {
  name: string;
  birth?: {
    place: string;
    date: Date;
  };
}

Now both are present or neither is. See Item 33.

Avoid "God Object" Interfaces

// Bad: too many optionals
interface UserProfile {
  id: string;
  name?: string;
  email?: string;
  avatar?: string;
  preferences?: Preferences;
  settings?: Settings;
  lastLogin?: Date;
  // ... 20 more optional fields
}

// Better: compose specific types
interface User {
  id: string;
  name: string;
}

interface UserWithProfile extends User {
  email: string;
  avatar: string;
}

interface UserWithPreferences extends User {
  preferences: Preferences;
}

Pressure Resistance Protocol

1. "Optional is Easier"

Pressure: "Just make it optional, then we don't have to change existing code"

Response: You're trading short-term convenience for long-term complexity.

Action: Evaluate: is absence meaningful, or just convenient?

2. "It Might Not Always Be Present"

Pressure: "The data isn't always available"

Response: Model that explicitly with a union type or separate interface.

Action: Make the state explicit, not implicit via optionality.

Red Flags - STOP and Reconsider

  • More than 3-4 optional properties on an interface
  • Optional properties that must be checked together
  • Comments explaining "if X is set, then Y must be too"
  • ?. chains accessing optional properties repeatedly

Common Rationalizations (All Invalid)

ExcuseReality
"It's backward compatible"Create a new type version instead
"Not all users need it"Different users = different types
"It's just one more optional"Each one doubles complexity

Quick Reference

// DON'T: Many independent optionals
interface Bad {
  a?: number;
  b?: string;
  c?: boolean;
  d?: Date;
}

// DO: Required with defaults
interface Good {
  a: number;
  b: string;
}
const good: Good = { a: 1, b: '', ...overrides };

// DO: Group related properties
interface Person {
  name: string;
  contact?: { email: string; phone: string };
}

// DO: Use union for different shapes
type Value = SimpleValue | DetailedValue;

The Bottom Line

Optional properties create hidden complexity.

Each optional property doubles the number of possible states. Prefer required properties with defaults, grouped optional objects, or explicit union types. Use optional properties only when absence is a meaningful, independent state.

Reference

Based on "Effective TypeScript" by Dan Vanderkam, Item 37: Limit the Use of Optional Properties.

适合场景

01

用户想查找某类 Agent Skill 时

02

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

03

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

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

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

能力 4

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

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

平台分布

Codex

33.9%
按下载量换算24

Claude

30.49%
按下载量换算22

Cursor

17.74%
按下载量换算13

Gemini CLI

8.99%
按下载量换算6

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

只读

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

安装前确认

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

来源信息

继续浏览同类 Skills