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

prefer-unknown-over-any宁愿未知胜过任何

Agent Skill

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

总安装

210

周安装

9

GitHub Stars

2

下载量

73
CodexClaudeCursorGemini CLI

安装说明

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

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

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

请帮我安装这个 Agent Skill:prefer-unknown-over-any(宁愿未知胜过任何)
来源仓库:https://github.com/marius-townhouse/effective-typescript-skills
仓库路径:skills/prefer-unknown-over-any
安装命令:
npx skills add https://github.com/marius-townhouse/effective-typescript-skills --skill prefer-unknown-over-any
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

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

skills.shnpx skills
npx skills add https://github.com/marius-townhouse/effective-typescript-skills --skill prefer-unknown-over-any

简介

宁愿未知胜过任何技能用于处理 GitHub 仓库、Issue 和 Pull Request 信息。

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

SKILL.md

Use unknown Instead of any for Values with an Unknown Type

Overview

unknown is the type-safe cousin of any.

When you don't know a value's type, use unknown. It forces you to narrow the type before using it, preventing bugs that any would allow.

When to Use This Skill

  • Parsing JSON or YAML
  • Receiving data from external sources
  • Working with user input
  • Writing functions that accept anything
  • Replacing any with something safer

The Iron Rule

NEVER use any when unknown would work.

No exceptions:

  • Not for "unknown is more verbose"
  • Not for "I know it's safe"
  • Not for "I'll check at runtime anyway"

Detection: The "any for Unknown Data" Smell

If you're using any because you don't know the type, use unknown instead.

// ❌ VIOLATION: any allows dangerous operations
function parseYAML(yaml: string): any {
  // ...
}

const data = parseYAML('...');
data.foo.bar.baz();        // No error! But will crash
data('call me maybe');     // No error! But will crash

// ✅ CORRECT: unknown forces you to check first
function safeParseYAML(yaml: string): unknown {
  // ...
}

const data = safeParseYAML('...');
data.foo.bar.baz();        // Error: 'data' is of type 'unknown'
data('call me maybe');     // Error: 'data' is of type 'unknown'

any vs unknown: The Key Difference

Propertyanyunknown
Anything assignable to itYesYes
Assignable to anythingYesNo
Can call methodsYes (danger!)No (safe!)
Must narrow before useNoYes

Using unknown Safely

With Type Assertion (when you're confident)

interface Book {
  name: string;
  author: string;
}

const data = safeParseYAML(`
  name: Wuthering Heights
  author: Emily Bronte
`) as Book;  // You assert it's a Book

console.log(data.name);  // OK

With instanceof (for classes)

function processValue(value: unknown) {
  if (value instanceof Date) {
    value.toISOString();  // OK - value is Date
  }
}

With typeof (for primitives)

function processValue(value: unknown) {
  if (typeof value === 'string') {
    value.toUpperCase();  // OK - value is string
  }
  if (typeof value === 'number') {
    value.toFixed(2);     // OK - value is number
  }
}

With Type Guards (for custom checks)

interface Book {
  name: string;
  author: string;
}

function isBook(value: unknown): value is Book {
  return (
    typeof value === 'object' &&
    value !== null &&
    'name' in value &&
    'author' in value
  );
}

function processValue(value: unknown) {
  if (isBook(value)) {
    console.log(value.name, value.author);  // OK - value is Book
  }
}

Common Use Cases

JSON Parsing

// ❌ BAD: JSON.parse returns any by default
const data = JSON.parse(response);
data.anything.goes();  // No error, but might crash

// ✅ GOOD: Parse as unknown, then validate
function parseJSON(text: string): unknown {
  return JSON.parse(text);
}

const data = parseJSON(response);
if (isValidResponse(data)) {
  // Now data is the validated type
}

API Responses

// ❌ BAD: Trusting the response
async function fetchUser(id: string): Promise<any> {
  const response = await fetch(`/api/users/${id}`);
  return response.json();
}

// ✅ GOOD: Type unknown, validate before use
async function fetchUser(id: string): Promise<unknown> {
  const response = await fetch(`/api/users/${id}`);
  return response.json();
}

const user = await fetchUser('123');
if (isUser(user)) {
  console.log(user.name);  // Safe!
}

Generic "Accept Anything" Functions

// ❌ BAD: any loses all type info
function isSmallArray(arr: any[]): boolean {
  return arr.length < 10;
}

// ✅ GOOD: unknown[] is safer
function isSmallArray(arr: readonly unknown[]): boolean {
  return arr.length < 10;
}

Narrowing unknown

TypeScript requires proof before it lets you use unknown:

function process(value: unknown) {
  // Direct use: Error
  value.toString();  // Error: 'value' is of type 'unknown'

  // After null check
  if (value !== null && value !== undefined) {
    // Still unknown, but non-nullish
  }

  // After typeof check
  if (typeof value === 'string') {
    value.toUpperCase();  // OK: value is string
  }

  // After instanceof check
  if (value instanceof Date) {
    value.toISOString();  // OK: value is Date
  }

  // After type guard
  if (isBook(value)) {
    value.author;  // OK: value is Book
  }
}

object, {}, and unknown

TypeIncludesDoes NOT include
unknownEverythingNothing excluded
{}Everything except null/undefinednull, undefined
objectObjects, arrays, functionsPrimitives, null, undefined
// unknown is most broad - prefer it for "I don't know"
function acceptAnything(value: unknown) {}

// {} excludes null/undefined
function acceptNonNullish(value: {}) {}

// object excludes primitives
function acceptObject(value: object) {}

Pressure Resistance Protocol

1. "unknown Is Too Verbose"

Pressure: "I have to add checks everywhere"

Response: Those checks catch bugs. any would let bugs through.

Action: Write the checks. They're documentation too.

2. "I Know The Type At Runtime"

Pressure: "I check the type before using it"

Response: With unknown, TypeScript verifies your checks are correct.

Action: Use unknown and let TypeScript track your narrowing.

3. "JSON.parse Returns any"

Pressure: "The built-in function returns any"

Response: Wrap it in a function that returns unknown.

Action: Create a safeParseJSON helper that returns unknown.

Red Flags - STOP and Reconsider

  • any for values from external sources
  • any for parsed JSON/YAML
  • any for API response bodies
  • any just because "I don't know the type"
  • Functions that accept any when they could accept unknown

Common Rationalizations (All Invalid)

ExcuseReality
"I check at runtime"unknown makes TypeScript verify your checks.
"It's too verbose"Verbosity catches bugs.
"any is simpler"Simpler to write, harder to debug.
"I trust the source"External sources are never trustworthy.

Quick Reference

SituationUse
Don't know the typeunknown
Parsing JSON/YAMLReturn unknown, then validate
External API dataunknown until validated
Accept any valueunknown (not any)
Array of anythingunknown[] (not any[])

The Bottom Line

unknown says "I don't know this type." any says "I don't care about types."

Use unknown when you genuinely don't know a value's type. It forces you to narrow before use, catching bugs that any would miss. Your code will be safer and more explicit about where type information comes from.

Reference

Based on "Effective TypeScript" by Dan Vanderkam, Item 46: Use unknown Instead of any for Values with an Unknown Type.

适合场景

01

用户想查找某类 Agent Skill 时

02

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

03

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

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

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

能力 4

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

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

平台分布

Codex

37.72%
按下载量换算28

Claude

27.89%
按下载量换算20

Cursor

19.12%
按下载量换算14

Gemini CLI

9.17%
按下载量换算7

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

external-service

该 Skill 可能调用第三方服务、云服务或外部模型 API,使用前需要确认账号、额度、数据发送范围和服务条款。

安装前确认

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

来源信息

继续浏览同类 Skills