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

generics-as-functions泛型作为函数

Agent Skill

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

总安装

196

周安装

8

GitHub Stars

2

下载量

63
CodexClaudeCursorGemini CLI

安装说明

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

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

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

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

命令行安装

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

skills.shnpx skills
npx skills add https://github.com/marius-townhouse/effective-typescript-skills --skill generics-as-functions

简介

用于处理 GitHub 仓库、Issue、Pull Request 和代码协作信息。

  • 适合在需要围绕仓库状态、代码变更或协作事项进行整理时使用。
  • 可结合来源仓库和原始 README 核验具体用法,支持 TypeScript 泛型函数开发辅助。
  • 安装命令:npx skills add https://github.com/marius-townhouse/effective-typescript-skills --skill generics-as-functions。
  • 注意确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写操作。

SKILL.md

Think of Generics as Functions Between Types

Overview

Generic types are the type-level equivalent of functions in value space. Just as a function takes parameters and returns a value, a generic type takes type parameters and produces a concrete type. This mental model helps you write better generic types by applying the same principles you use for writing functions: constraining inputs, choosing good names, and documenting behavior.

Understanding generics as functions between types clarifies when to use constraints, how to name type parameters, and why some generic patterns work while others don't. This perspective is essential for effective type-level programming in TypeScript.

When to Use This Skill

  • Defining generic types that transform other types
  • Writing generic functions with type parameters
  • Constraining what types can be passed to generics
  • Documenting generic types with TSDoc
  • Creating reusable type utilities

The Iron Rule

Think of generic types as functions between types: use extends to constrain inputs like type annotations, choose descriptive names, and document with @template TSDoc.

Detection

Watch for these patterns:

// RED FLAGS - Poor generic design
type MyPick<T, K> = { [P in K]: T[P] };  // No constraints, errors in implementation
type BadGeneric<X, Y, Z> = ...;  // Single-letter names without context
function parse<T>(input: string): T;  // Return-only generic, no better than any

Generic Types as Functions

A generic type takes type parameters and produces a concrete type:

// Generic type "function"
type MyPartial<T> = { [K in keyof T]?: T[K] };

// "Calling" the function with Person
type PartPerson = MyPartial<Person>;
// Equivalent to: { name?: string; age?: number; }

Just like functions, generic types can have multiple parameters:

// Two type parameters
type MyPick<T, K extends keyof T> = { [P in K]: T[P] };

// Usage
type NameOnly = MyPick<Person, 'name'>;
// Equivalent to: { name: string }

Constraining Type Parameters

Use extends to constrain type parameters, just as you'd use type annotations for function parameters:

// GOOD: Constrained type parameters
type MyPick<T extends object, K extends keyof T> = {
  [P in K]: T[P]
};

// Without constraints - allows invalid instantiations
type BadPick<T, K> = { [P in K]: T[P] };  // Errors in implementation

// Invalid uses caught by constraints:
type Bad1 = MyPick<Person, 'firstName'>;  // Error: 'firstName' not in Person
type Bad2 = MyPick<'age', Person>;  // Error: string doesn't satisfy object

Naming Type Parameters

Choose descriptive names, especially for complex generics:

// Short names OK for simple, local generics
type Partial<T> = { [K in keyof T]?: T[K] };

// Longer names for complex or exported generics
type MapValues<
  ObjectType extends object,
  ValueTransformer extends (value: any) => any
> = {
  [Key in keyof ObjectType]: ValueTransformer<ObjectType[Key]>
};

Documenting Generics

Use @template TSDoc tag to document type parameters:

/**
 * Construct a new object type using a subset of properties from another.
 * @template T - The original object type
 * @template K - The keys to pick, typically a union of string literal types
 */
type MyPick<T extends object, K extends keyof T> = {
  [P in K]: T[P]
};

Generic Functions

Generic functions define associated generic types and enable type inference:

function pick<T extends object, K extends keyof T>(
  obj: T,
  ...keys: K[]
): Pick<T, K> {
  const result: Partial<Pick<T, K>> = {};
  for (const k of keys) {
    result[k] = obj[k];
  }
  return result as Pick<T, K>;
}

// TypeScript infers types from arguments
const person = { name: 'Alice', age: 30 };
const nameOnly = pick(person, 'name');
// Type: Pick<{ name: string; age: number }, 'name'>

Generic Classes

Generic classes capture types that don't need to be passed to methods:

class Box<T> {
  value: T;
  constructor(value: T) {
    this.value = value;
  }
  getValue(): T {
    return this.value;
  }
}

// Type inferred from constructor
const dateBox = new Box(new Date());
// Type: Box<Date>

Pressure Resistance Protocol

When pressured to use unconstrained generics:

  1. Add constraints: Use extends to limit valid type arguments
  2. Consider defaults: Provide sensible defaults for type parameters
  3. Document requirements: Use TSDoc to explain constraints
  4. Test edge cases: Verify generics work with unions and edge cases

Red Flags

Anti-PatternWhy It's Bad
Unconstrained type parametersAllows invalid instantiations, implementation errors
Single-letter names in complex genericsReduces readability
Return-only genericsEquivalent to type assertions, no type safety
Missing TSDoc on public genericsPoor developer experience

Common Rationalizations

"Constraints limit flexibility"

Reality: Constraints catch errors at the type level rather than producing confusing type errors or wrong types. They document valid usage.

"T, K, V are standard names"

Reality: They are conventional for simple cases, but descriptive names improve readability in complex generics. Match name length to scope.

"Users can figure out the types"

Reality: Documentation helps users understand generics without reading implementation. @template tags appear in IDE tooltips.

Quick Reference

ConceptValue-LevelType-Level
Definitionfunctiontype
Parameters(x: T)<T extends Constraint>
Return: ReturnType= ResultType
Documentation@param@template
ConstraintsType annotationsextends keyword

The Bottom Line

Generic types are functions between types. Apply the same principles you use for writing functions: constrain inputs, choose meaningful names, and document thoroughly. This mental model makes complex type-level code more approachable and maintainable.

Reference

  • Effective TypeScript, 2nd Edition by Dan Vanderkam
  • Item 50: Think of Generics as Functions Between Types

适合场景

01

用户想查找某类 Agent Skill 时

02

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

03

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

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

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

能力 4

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

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

平台分布

Codex

34.65%
按下载量换算22

Claude

30.07%
按下载量换算19

Cursor

17.89%
按下载量换算11

Gemini CLI

9.1%
按下载量换算6

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

只读

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

安装前确认

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

来源信息

继续浏览同类 Skills