Skip to content
Skills Library
Programming · Learning & explanation

typescript-advanced-type-system

/typescript-advanced-type-system

Advanced TypeScript type-system mechanics — union and intersection types, conditional types with `infer`, mapped types, template literal types, generics and their constraints, index signatures, type assertions, and extracting types from other types (return types, array elements, promise results). Use when designing a complex generic, modeling a type that transforms based on another type, extracting a type from a function or structure with `infer`, or deciding between a type assertion and a real narrowing check.

Repository
tekhne ↗
Licence
MIT
Last repository change
21.10.2026

Assessment and security

95/ 100Class: good
Format20 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
23
Scripts
0
HTML comments
0
URLs in SKILL.md
3
Deprecation

No signals detected. Repository changed on 21.10.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Advanced TypeScript type-system mechanics — union and intersection types, conditional types with `infer`, mapped types, template literal types, generics and their constraints, index signatures, type assertions, and extracting types from other types (return types, array elements, promise results). Use when designing a complex generic, modeling a type that transforms based on another type, extracting a type from a function or structure with `infer`, or deciding between a type assertion and a real narrowing check.

Original author description

Advanced TypeScript type-system mechanics — union and intersection types, conditional types with `infer`, mapped types, template literal types, generics and their constraints, index signatures, type assertions, and extracting types from other types (return types, array elements, promise results). Use when designing a complex generic, modeling a type that transforms based on another type, extracting a type from a function or structure with `infer`, or deciding between a type assertion and a real narrowing check.

How it works

  1. Mindset
  2. When to Apply
  3. Use When
  4. Scope
  5. When NOT to Use
  6. Quick Commands
  7. Anti-Patterns
  8. References
Original SKILL.md headings · 135 lines, 902 words
SKILL.md preview
---
name: typescript-advanced-type-system
description: Advanced TypeScript type-system mechanics — union and intersection types, conditional types with `infer`, mapped types, template literal types, generics and their constraints, index signatures, type assertions, and extracting types from other types (return types, array elements, promise results). Use when designing a complex generic, modeling a type that transforms based on another type, extracting a type from a function or structure with `infer`, or deciding between a type assertion and a real narrowing check.
allowed-tools: Read, Write, Edit, Bash
---

# TypeScript Type System

The type-level mechanics that everything else in this skill family builds on: unions, conditionals, mapped types, template literals, generics, and type-level extraction with `infer`.

## Mindset

Model the problem in types before reaching for a runtime check — a type that makes an invalid combination unrepresentable is a compile-time guarantee, while a runtime check for the same thing only protects the paths that get exercised. Prefer deriving a type from another type (`ReturnType<typeof fn>`, a mapped type, a conditional extraction with `infer`) over hand-writing a parallel type that can drift out of sync with the thing it describes. When a conditional type distributes over a union unexpectedly, that is usually the type system doing exactly what it's told — wrap the checked type in a tuple (`[T] extends [U]`) to opt out of distribution rather than restructuring the type to dodge it. A type assertion (`as`) is a claim, not a check; only reach for one when you have already established the fact some other way (e.g. immediately after a runtime guard), never as a way to silence an error you haven't investigated.

## When to Apply

Use this skill when:

- Modeling a union or intersection type, including discriminated unions at the type-definition level
- Writing a conditional type, especially one using `infer` to extract a nested type
- Building a mapped type or remapping its keys with an `as` clause
- Constructing a template literal type (e.g. typed route parameters)
- Designing a generic function or type with meaningful constraints
- Extracting a type from a function, array, promise, or constructor
- Deciding whether a value needs an index signature or a fixed shape
- Choosing between a type assertion and a real narrowing check

## Use When

- "How do I extract the return type / element type / awaited type from this?"
- "How should I model this union or generic type?"
- "How do I build a type that changes shape based on another type?"
- "What's the difference between `as` and a type guard here?"

## Scope

### In Scope

- Union, intersection, conditional, mapped, and template literal types.
- Generics and their constraints.
- Index signatures and type assertions.
- Type-level extraction with `infer` (return types, array elements, promise values, tuple destructuring, constructor/instance types).

### Out of Scope

- Runtime narrowing and type guards (`typeof`, `instanceof`, custom predicates) — see the sibling `typescript-type-guards` skill, which also owns exhaustiveness checking.
- Built-in utility types that wrap these mechanics for common cases (`Partial`, `Pick`, `ReturnType` as a ready-made utility) — see the sibling `typescript-utility-types` skill for the applied, ready-to-use versions.
- Design patterns that use these mechanics structurally — see the sibling `typescript-design-patterns` skill.

## When NOT to Use

Do not use this skill for a runtime check — narrowing a value at runtime with `typeof`/`instanceof`/a custom predicate belongs in `typescript-type-guards`. Do not use it as a substitute for `tsc --noEmit`; a type that looks correct in isolation can still fail to apply as expected against real call sites.

## Quick Commands

### Type Check

```bash
npx tsc --noEmit
```

### Type Check Single Entry

```bash
npx tsc --noEmit src/index.ts
```

## Anti-Patterns

### NEVER silence a type error with an unchecked assertion instead of modeling the type correctly

**WHY:** an `as` assertion bypasses the compiler without a matching runtime guarantee; the error it silences almost always points at a type that needs to be modeled more precisely, not asserted away.

**BAD**:

```typescript
const id = getValue() as string;
```

**GOOD**:

```typescript
type Result = { kind: "ok"; value: string } | { kind: "error"; message: string };
function unwrap(result: Result): string {
  if (result.kind === "error") throw new Error(result.message);
  return result.value;
}
```

### NEVER hand-write a type that duplicates what `infer` or a mapped type could derive

**WHY:** a hand-written parallel type drifts out of sync with the source it was copied from the first time either one changes.

**BAD**:

```typescript
function getUser() { return { id: 1, name: "Alice" }; }
interface User { id: number; name: string; } // duplicates getUser's actual return shape
```

**GOOD**:

```typescript
function getUser() { return { id: 1, name: "Alice" }; }
type User = ReturnType<typeof getUser>; // stays correct if getUser changes
```

### NEVER leave a generic parameter unconstrained when the type only makes sense for a shape

**WHY:** an unconstrained `<T>` accepts anything, including values the type's own logic assumes won't appear.

**BAD**:

```typescript
type PropType<T, K> = T extends { [P in K]: infer V } ? V : never; // K unconstrained
```

**GOOD**:

```typescript
type PropType<T, K extends keyof T> = T extends { [P in K]: infer V } ? V : never;
```

## References

| File | Covers |
| --- | --- |
| `references/unions-intersections.md` | Union and intersection types |
| `references/conditional-types.md` | Conditional types, `infer`, distributive behavior |
| `references/mapped-types.md` | Mapped types and key remapping mechanics |
| `references/template-literal-types.md` | Template literal types (e.g. typed route parameters) |
| `references/generics.md` | Generic constraints and type parameters |
| `references/index-signatures.md` | Index signatures and dynamic properties |
| `references/type-assertions.md` | `as` assertions and type compatibility |
| `references/infer-extraction.md` | Extracting return types, parameters, array elements, promise values, tuple elements, and instance/`this` types with `infer` |

- [TypeScript Handbook: Conditional Types](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html)
- [TypeScript Handbook: Mapped Types](https://www.typescriptlang.org/docs/handbook/2/mapped-types.html)
- [TypeScript Handbook: Template Literal Types](https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html)
Files in this package (23)
  • SKILL.md 7 KB
  • .audits/2026-09-23/Analysis.md 1 KB
  • .audits/2026-09-23/Remediation.md 1 KB
  • .audits/2026-09-23/audit.json 1 KB
  • evals/instructions.json 2 KB
  • evals/summary.json 1 KB
  • evals/scenario-0/capability.txt 1 KB
  • evals/scenario-0/criteria.json 1 KB
  • evals/scenario-0/task.md 1 KB
  • evals/scenario-1/capability.txt 1 KB
  • evals/scenario-1/criteria.json 1 KB
  • evals/scenario-1/task.md 1 KB
  • evals/scenario-2/capability.txt 1 KB
  • evals/scenario-2/criteria.json 1 KB
  • evals/scenario-2/task.md 1 KB
  • references/conditional-types.md 5 KB
  • references/generics.md 4 KB
  • references/index-signatures.md 4 KB
  • references/infer-extraction.md 3 KB
  • references/mapped-types.md 5 KB
  • references/template-literal-types.md 5 KB
  • references/type-assertions.md 3 KB
  • references/unions-intersections.md 3 KB

Versions

The database has no other skills named typescript-advanced-type-system.

Database snapshot: 13.11.2026 · package version 102af32999d9 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100typescript-advanced-types
    Master TypeScript's advanced type system including generics, conditional types, mapped types, template literals, and utility types for building type-safe applications. Use when implementing complex...
  • 95typescript-advanced-types
    Master TypeScript's advanced type system including generics, conditional types, mapped types, template literals, and utility types for building type-safe applications. Use when implementing complex type logic, creating reusable type utilities, or ensuring compile-time type…
  • 95typescript-advanced-types
    Master TypeScript's advanced type system including generics, conditional types, mapped types, template literals, and utility types for building type-safe applications. Use when implementing complex type logic, creating reusable type utilities, or ensuring compile-time type…
  • 95typescript-advanced-types
    Master TypeScript's advanced type system including generics, conditional types, mapped types, template literals, and utility types for building type-safe applications. Use when implementing complex type logic, creating reusable type utilities, or ensuring compile-time type…
  • 100typescript-type-hardening
    Use when TypeScript has type errors, failing inference, or needs type mechanism repair (generics, infer, mapped, branded). Not for strict flags or proactive shaping: use typescript-best-practices.
  • 100typescript-expert
    Expert TypeScript: type system, generics, narrowing, inference, and strict-mode safety. Trigger keywords: TypeScript, types, generics, type narrowing, tsconfig, strict, discriminated union, conditional/mapped types, utility types, declaration files, type error, satisfies, infer,…

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.