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

prefer-type-annotations更喜欢类型注释

Agent Skill

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

总安装

222

周安装

9

GitHub Stars

2

下载量

70
CodexClaudeCursorGemini CLI

安装说明

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

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

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

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

命令行安装

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

skills.shnpx skills
npx skills add https://github.com/marius-townhouse/effective-typescript-skills --skill prefer-type-annotations

简介

更喜欢类型注释技能用于处理 GitHub 仓库、Issue 和 Pull Request 信息。

  • 适用于 Codex、Claude、Cursor、Gemini CLI 等宿主环境。
  • 通过 GitHub 安装,使用 npx skills add 命令添加指定仓库的技能。
  • 需确认权限范围和维护状态,注意可能触发联网或文件读写操作。
  • prefer-type-annotations 属于前端设计类 Skill,可作为该场景下的辅助能力补充。

SKILL.md

Prefer Type Annotations to Type Assertions

Overview

Type annotations (: Type) verify that values conform to types. Type assertions (as Type) tell TypeScript to trust you.

Annotations check your work. Assertions bypass checks. When you have a choice, prefer annotations.

When to Use This Skill

  • Assigning values to variables
  • Defining function return types
  • Working with object literals
  • Tempted to use as Type to fix errors
  • Getting "not assignable" errors

The Iron Rule

NEVER use type assertions when type annotations would work.

No exceptions:

  • Not for "it's obviously correct"
  • Not for "I know better than TypeScript"
  • Not for "the assertion is simpler"

Detection: The "as" Smell

See as SomeType? Ask: "Could I use a type annotation instead?"

// ❌ VIOLATION: Type assertion bypasses checking
const alice = {} as Person;  // No error! But alice has no properties

// ✅ CORRECT: Type annotation verifies the value
const alice: Person = {};
// ~~~~~ Property 'name' is missing in type '{}' but required in type 'Person'

The Critical Difference

Type Annotation (Safe)

interface Person { name: string }

// TypeScript CHECKS that the value matches the type
const bob: Person = { name: 'Bob' };  // OK
const bad: Person = {};                // Error: missing 'name'
const extra: Person = {
  name: 'Carol',
  age: 30           // Error: 'age' does not exist on type 'Person'
};

Type Assertion (Unsafe)

// TypeScript TRUSTS you that the value matches the type
const bob = {} as Person;       // No error, but wrong!
const bad = { foo: 1 } as Person;  // No error, but wrong!

Arrow Functions: Annotate Returns

// ❌ VIOLATION: Assertion hides errors
const people = ['alice', 'bob'].map(name => ({name} as Person));

// Even worse: completely wrong values slip through
const people = ['alice', 'bob'].map(name => ({} as Person));  // No error!

// ✅ CORRECT: Annotate the return type
const people = ['alice', 'bob'].map((name): Person => ({name}));  // OK
const people = ['alice', 'bob'].map((name): Person => ({}));
//                                                     ~~ Error: missing 'name'

When Assertions ARE Appropriate

Type assertions make sense when you truly know more than TypeScript:

1. DOM Elements

// You know #myButton exists and is a button
const button = document.querySelector('#myButton') as HTMLButtonElement;

// Better: include a comment explaining why
const button = document.querySelector('#myButton') as HTMLButtonElement;
// This button is created in index.html and always exists

2. After Runtime Checks

const el = document.getElementById('foo');
if (el) {
  el.innerHTML = 'Hello';  // TypeScript knows el is not null
}

// Or with non-null assertion (use sparingly!)
const el = document.getElementById('foo')!;

Pressure Resistance Protocol

1. "The Error Is Wrong"

Pressure: "TypeScript is complaining but my code is correct"

Response: TypeScript is usually right. Read the error message carefully.

Action:

  1. Check if your type definition matches your intention
  2. Check if your value actually matches the type
  3. Only use assertion if you can explain WHY TypeScript is wrong

2. "The Assertion Is Simpler"

Pressure: "Adding annotations everywhere is verbose"

Response: Annotations catch bugs. Assertions hide them. Safety > brevity.

Action: Add the annotation. Your future self will thank you.

3. "I Know The Type At Runtime"

Pressure: "I checked the type at runtime, so assertion is safe"

Response: If you checked at runtime, TypeScript should be able to narrow the type.

Action: Use a type guard or conditional to narrow, not an assertion.

Red Flags - STOP and Reconsider

  • as any anywhere in your code
  • as Type immediately after creating an object
  • Multiple assertions in a chain (x as A as B)
  • Assertions to fix "not assignable" errors
  • ! (non-null assertion) without good reason

Common Rationalizations (All Invalid)

ExcuseReality
"I know it's a Person"Then prove it with an annotation, not an assertion.
"TypeScript is wrong"TypeScript found a real inconsistency. Investigate.
"It's just one assertion"Assertions spread. One leads to many.
"The types are too strict"Strict types catch bugs. Embrace them.

Quick Reference

SituationUse AnnotationUse Assertion
Variable declaration:TypeNever
Function parameter:TypeNever
Function return:TypeRarely
Object literal:TypeNever
DOM element you know-as HTMLElement with comment
After null check-! if certain
Unknown value from API-After validation with unknown

The Non-Null Assertion (!)

// Tells TypeScript: "Trust me, this isn't null"
const el = document.getElementById('foo')!;

// Prefer a runtime check:
const el = document.getElementById('foo');
if (!el) throw new Error('No element #foo');
// Now TypeScript knows el is not null

The Bottom Line

Type annotations are a contract. Type assertions are a lie you tell TypeScript.

Use annotations to declare your intentions. Use assertions only when you have information TypeScript cannot have. Always include a comment explaining why an assertion is valid.

Reference

Based on "Effective TypeScript" by Dan Vanderkam, Item 9: Prefer Type Annotations to Type Assertions.

适合场景

01

用户想查找某类 Agent Skill 时

02

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

03

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

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

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

能力 4

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

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

平台分布

Codex

36.29%
按下载量换算25

Claude

30.16%
按下载量换算21

Cursor

20.2%
按下载量换算14

Gemini CLI

8.85%
按下载量换算6

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

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

安装前确认

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

来源信息

继续浏览同类 Skills