🎯

structural-typing

🎯Skill

from marius-townhouse/effective-typescript-skills

VibeIndex|
What it does

Enables understanding and leveraging TypeScript's structural typing by revealing how types are compared based on shape, not explicit declaration.

πŸ“¦

Part of

marius-townhouse/effective-typescript-skills(83 items)

structural-typing

Installation

Quick InstallInstall with npx
npx skills add marius-townhouse/effective-typescript-skills --all
Quick InstallInstall with npx
npx skills add marius-townhouse/effective-typescript-skills -s prefer-unknown-over-any exhaustiveness-checking
Quick InstallInstall with npx
npx skills add marius-townhouse/effective-typescript-skills -a opencode claude-code
Quick InstallInstall with npx
npx skills add marius-townhouse/effective-typescript-skills -l
git cloneClone repository
git clone https://github.com/marius-townhouse/effective-typescript-skills.git
πŸ“– Extracted from docs: marius-townhouse/effective-typescript-skills
1Installs
-
AddedFeb 4, 2026

Skill Details

SKILL.md

Use when surprised by TypeScript accepting unexpected values. Use when designing function parameters. Use when testing with mock objects.

Overview

# Get Comfortable with Structural Typing

Overview

TypeScript uses structural typing: if it has the right shape, it fits.

Unlike nominal typing (where types must be explicitly declared), TypeScript checks structure. Understanding this prevents surprises and unlocks powerful patterns.

When to Use This Skill

  • Surprised that TypeScript accepts "wrong" values
  • Designing interfaces and function parameters
  • Writing unit tests with mock objects
  • Debugging "impossible" type errors
  • Understanding why extra properties are allowed

The Iron Rule

```

NEVER assume types are "sealed" - they always allow extra properties.

```

Accept that:

  • If it has the required properties, it's assignable
  • Extra properties don't make a value invalid
  • Classes are compared by structure, not identity

Detection: The "Sealed Type" Assumption

If you're surprised that TypeScript accepts a value, you're probably assuming nominal typing.

```typescript

interface Vector2D {

x: number;

y: number;

}

function calculateLength(v: Vector2D) {

return Math.sqrt(v.x 2 + v.y 2);

}

// βœ… Works as expected

calculateLength({ x: 3, y: 4 }); // 5

// βœ… Also works! Has x and y, so it's a valid Vector2D

const namedVector = { x: 3, y: 4, name: 'Pythagoras' };

calculateLength(namedVector); // 5

// βœ… Even 3D vectors work (but give wrong results!)

const vector3D = { x: 3, y: 4, z: 5 };

calculateLength(vector3D); // 5 (ignores z!)

```

The Structural Typing Principle

A value is assignable to a type if it has at least the required properties with compatible types.

```typescript

interface Point {

x: number;

y: number;

}

// All of these are valid Points:

const p1: Point = { x: 1, y: 2 }; // Exact match

const p2: Point = { x: 1, y: 2, z: 3 }; // Extra property (via variable)

const p3: Point = { x: 1, y: 2, name: 'origin' }; // Different extra property

// But not this:

const p4: Point = { x: 1 }; // Error: missing 'y'

```

Why This Matters for Functions

```typescript

interface Vector2D { x: number; y: number; }

interface Vector3D { x: number; y: number; z: number; }

function normalize(v: Vector3D) {

const length = Math.sqrt(v.x 2 + v.y 2 + v.z ** 2);

return {

x: v.x / length,

y: v.y / length,

z: v.z / length,

};

}

// This is a bug, but TypeScript doesn't catch it:

function calculateLength2D(v: Vector2D) {

return Math.sqrt(v.x 2 + v.y 2);

}

// normalize calls calculateLength2D internally

function normalize(v: Vector3D) {

const length = calculateLength2D(v); // Bug: ignores z!

// Vector3D is assignable to Vector2D

}

```

Structural Typing with Classes

```typescript

class SmallContainer {

num: number;

constructor(num: number) {

if (num < 0 || num >= 10) {

throw new Error('Must be 0-9');

}

this.num = num;

}

}

const a = new SmallContainer(5); // OK

// This also type-checks, but bypasses validation!

const b: SmallContainer = { num: 2024 }; // No error!

// Because SmallContainer structurally is just { num: number }

```

Benefits: Easy Testing

Structural typing makes testing simpler - no mocking libraries needed:

```typescript

interface Database {

runQuery(sql: string): any[];

}

function getUsers(db: Database) {

return db.runQuery('SELECT * FROM users');

}

// In tests, just create an object with the right shape:

test('getUsers', () => {

const mockDb = {

runQuery(sql: string) {

return [{ name: 'Alice' }, { name: 'Bob' }];

}

};

const users = getUsers(mockDb); // Works! No type error

expect(users).toHaveLength(2);

});

```

The "Excess Property Checking" Exception

Object literals get special treatment - TypeScript flags extra properties:

```typescript

interface Point { x: number; y: number; }

// Extra property in object literal: Error!

const p: Point = { x: 1, y: 2, z: 3 };

// ~ Object literal may only specify known properties

// But via intermediate variable: No error

const temp = { x: 1, y: 2, z: 3 };

const p: Point = temp; // OK

```

This is a usability feature, not a change in structural typing rules. See the excess-property-checking skill for details.

When Structural Typing Causes Problems

Problem: Wrong Vector Dimension

```typescript

// Solution 1: Use optional never to forbid property

interface Vector2D {

x: number;

y: number;

z?: never; // Explicitly disallows z

}

// Solution 2: Use branded types (see branded-types skill)

type Vector2D = { x: number; y: number } & { _brand: 'Vector2D' };

```

Problem: Class Validation Bypassed

```typescript

// Solution: Make the class have unique properties

class SmallContainer {

private readonly _brand = 'SmallContainer'; // Can't be faked

num: number;

// ...

}

```

Pressure Resistance Protocol

1. "This Shouldn't Be Allowed"

Pressure: "TypeScript should reject values with extra properties"

Response: That's nominal typing. TypeScript uses structural typing.

Action: Use techniques like branded types if you need stricter checking.

2. "My Class Should Be Special"

Pressure: "Only real instances of my class should be valid"

Response: Classes are structurally typed. Add private fields to differentiate.

Action: Use private fields or brands for nominal-like behavior.

Red Flags - STOP and Reconsider

  • Assuming extra properties make a value invalid
  • Expecting class identity to matter
  • Surprised when TypeScript accepts "wrong" values
  • Thinking types are "sealed"
  • Validation logic that TypeScript doesn't see

Common Rationalizations (All Invalid)

| Excuse | Reality |

|--------|---------|

| "It's not the right type" | If it has the right shape, it is. |

| "My class validates" | Structural objects bypass the constructor. |

| "Extra props shouldn't work" | In TypeScript, they do. |

Quick Reference

| Scenario | Structural Typing Behavior |

|----------|---------------------------|

| Extra properties on values | Allowed (except object literals) |

| Class instances | Compared by structure, not class identity |

| Function parameters | Any structurally compatible value works |

| Object literal assignment | Excess properties flagged (special case) |

The Bottom Line

TypeScript checks shape, not identity.

If a value has all the required properties with compatible types, it's assignable. This enables easy testing and flexible APIs, but can cause surprises. Use techniques like branded types when you need stricter checking.

Reference

Based on "Effective TypeScript" by Dan Vanderkam, Item 4: Get Comfortable with Structural Typing.

More from this repository10

🎯
tsdoc-comments🎯Skill

Generates TypeScript documentation comments (TSDoc) to explain public APIs, complex types, and provide comprehensive code documentation with IDE tooltips.

🎯
async-over-callbacks🎯Skill

Transforms callback-based asynchronous code into clean, readable async/await patterns for better type flow and error handling.

🎯
type-safe-monkey-patching🎯Skill

Enables type-safe runtime extension of global objects and DOM elements in TypeScript without sacrificing type checking or using `as any`.

🎯
create-objects-all-at-once🎯Skill

Efficiently initializes multiple TypeScript objects simultaneously using concise object literal syntax and spread operators.

🎯
module-by-module-migration🎯Skill

Guides developers through systematic TypeScript module migration, breaking down complex refactoring into manageable, incremental steps.

🎯
ts-js-relationship🎯Skill

Explains TypeScript's relationship to JavaScript, highlighting how it adds static typing and catches errors before runtime while remaining fully compatible with JavaScript code.

🎯
code-gen-independent🎯Skill

Generates JavaScript code despite TypeScript type errors and demonstrates that TypeScript types are erased at runtime, requiring alternative type checking strategies.

🎯
context-type-inference🎯Skill

Helps restore precise type context when extracting values, preventing type inference errors through annotations, const assertions, and type preservation techniques.

🎯
precise-string-types🎯Skill

Enforces strict string type constraints and prevents unintended string type conversions in TypeScript projects.

🎯
type-display-attention🎯Skill

Displays and simplifies complex TypeScript types to improve IDE readability and developer experience.