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

type-vs-interface类型与接口

Agent Skill

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

总安装

188

周安装

8

GitHub Stars

2

下载量

66
CodexClaudeCursorGemini CLI

安装说明

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

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

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

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

命令行安装

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

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

简介

用于处理 GitHub 仓库、Issue、Pull Request 和代码协作信息,辅助项目协作管理。

  • 适合在 Codex、Claude、Cursor、Gemini CLI 中围绕代码变更和协作事项进行整理。
  • 通过 GitHub 安装,使用 npx skills add 命令从 marius-townhouse/effective-typescript-skills 仓库添加技能。
  • 安装前需确认权限范围和维护状态,避免触发不必要的联网或文件操作。
  • type-vs-interface 属于前端设计类 Skill,可作为该场景下的辅助能力补充。

SKILL.md

Know the Differences Between type and interface

Overview

Use interface for object types, type for everything else.

Both type and interface can define object types, but they have different capabilities. Understanding these differences helps you choose the right tool and write consistent code.

When to Use This Skill

  • Defining any named type
  • Choosing between type alias and interface
  • Extending or composing types
  • Working with declaration files
  • Understanding library type definitions

The Iron Rule

Use INTERFACE for object types that could be extended.
Use TYPE for unions, tuples, mapped types, and complex compositions.

Remember:

  • Interfaces support declaration merging
  • Type aliases can express more complex types
  • Both can be extended and implemented
  • Consistency within a codebase matters most

Quick Decision Guide

ScenarioUse
Object type (API response, props)interface
Union typetype
Tuple typetype
Function typetype
Mapped typetype
Primitive aliastype
Library types meant to be extendedinterface

The Similarities

Both Define Object Shapes

type TState = {
  name: string;
  capital: string;
};

interface IState {
  name: string;
  capital: string;
}
// These are interchangeable for most purposes

Both Support Generics

type TBox<T> = { value: T };
interface IBox<T> { value: T }

Both Can Be Extended

// Interface extending type
interface IStateWithPop extends TState {
  population: number;
}

// Type extending interface
type TStateWithPop = IState & { population: number };

Both Can Be Implemented

class StateImpl implements IState {
  name = '';
  capital = '';
}

The Differences

1. Union Types (type only)

type StringOrNumber = string | number;  // Only with type
type Status = 'pending' | 'fulfilled' | 'rejected';

// Can't do this with interface

2. Tuple Types (type only)

type Pair = [number, number];
type NamedNums = [string, ...number[]];

// Interface syntax is awkward
interface IPair {
  0: number;
  1: number;
  length: 2;
}

3. Declaration Merging (interface only)

interface User {
  name: string;
}

interface User {
  email: string;
}

// Now User has both name and email
const user: User = { name: 'Alice', email: 'alice@example.com' };

This is how TypeScript extends standard library types across ES versions.

4. Better Error Messages (interface)

interface Person {
  name: string;
  age: string;  // Note: string
}

// Type intersection silently creates unusable type
type TPerson = Person & { age: number };  // No error, but age is never

// Interface extension gives helpful error
interface IPerson extends Person {
  age: number;
  // ~~~ Types of property 'age' are incompatible
}

5. Type Alias Inlining

TypeScript may inline type aliases in error messages and.d.ts files:

// In generated .d.ts, type aliases may be expanded
type Point = { x: number; y: number };
export function getOrigin(): Point { ... }

// May become:
export function getOrigin(): { x: number; y: number };

// Interfaces are preserved by name
interface IPoint { x: number; y: number }
export function getOrigin(): IPoint { ... }

// Stays as:
export function getOrigin(): IPoint;

When to Use interface

Defining Public APIs

// Users might want to extend this
export interface RequestOptions {
  url: string;
  method?: string;
  headers?: Record<string, string>;
}

// Declaration merging allows extension
declare module 'my-lib' {
  interface RequestOptions {
    timeout?: number;  // User adds this
  }
}

Object Types in General

interface User {
  id: string;
  name: string;
  email: string;
}

interface Post {
  id: string;
  title: string;
  author: User;
}

When to Use type

Union Types

type Result<T> = { ok: true; value: T } | { ok: false; error: Error };
type Status = 'loading' | 'success' | 'error';

Tuples and Arrays

type Point = [x: number, y: number];
type RGB = [number, number, number];

Function Types

type Handler = (event: Event) => void;
type AsyncFn<T> = () => Promise<T>;

Mapped and Conditional Types

type Readonly<T> = { readonly [K in keyof T]: T[K] };
type NonNullable<T> = T extends null | undefined ? never : T;

Computed Types

type Keys = keyof User;  // 'id' | 'name' | 'email'
type UserValues = User[keyof User];  // string

Extending Patterns

Interface extends Interface

interface Animal {
  name: string;
}

interface Dog extends Animal {
  breed: string;
}

Type intersects Type

type Animal = { name: string };
type Dog = Animal & { breed: string };

Mixing (works both ways)

// Interface extends type
interface Dog extends Animal { breed: string; }

// Type extends interface
type Cat = Animal & { meows: boolean };

Pressure Resistance Protocol

1. "Just Pick One and Stick With It"

Pressure: "Consistency matters more than the choice"

Response: True, but know WHY you're choosing.

Action: Default to interface for objects, type for everything else.

2. "I Need Union of Interfaces"

Pressure: "My interfaces should form a union"

Response: You can union interfaces with a type alias.

Action: type Either = InterfaceA | InterfaceB;

Red Flags - STOP and Reconsider

  • Using interface for tuple types
  • Using type when you need declaration merging
  • Inconsistent usage across similar types in a codebase

Common Rationalizations (All Invalid)

ExcuseReality
"They're exactly the same"No, declaration merging and union types differ
"Type is always better"Interface gives better errors and supports merging
"Interface is always better"Can't express unions, tuples, or mapped types

Quick Reference

// USE INTERFACE FOR:
interface User { name: string; }       // Object types
interface Config extends Base { }       // Extendable types

// USE TYPE FOR:
type ID = string | number;              // Unions
type Point = [number, number];          // Tuples
type Handler = () => void;              // Functions
type Keys = keyof User;                 // Computed types
type Mapped = { [K in Keys]: boolean }; // Mapped types

The Bottom Line

Use interface for object types, type for everything else.

Interfaces support declaration merging and produce better error messages. Type aliases are required for unions, tuples, and complex type operations. Be consistent within your codebase.

Reference

Based on "Effective TypeScript" by Dan Vanderkam, Item 13: Know the Differences Between type and interface.

适合场景

01

用户想查找某类 Agent Skill 时

02

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

03

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

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

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

能力 4

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

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

平台分布

Codex

37.48%
按下载量换算25

Claude

29.32%
按下载量换算19

Cursor

20.16%
按下载量换算13

Gemini CLI

10.07%
按下载量换算7

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

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

安装前确认

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

来源信息

继续浏览同类 Skills