CtrlK
BlogDocsLog inGet started
Tessl Logo

pantheon-ai/typescript-advanced

Comprehensive TypeScript guidance covering compiler configuration, advanced types, utility types, type guards, strict mode workflows, and documentation patterns; use when configuring tsconfig, designing complex generics, making illegal states unrepresentable, fixing type errors, or writing testable and maintainable type-safe APIs.

Overall
score

99%

Does it follow best practices?

Validation for skill structure

Overview
Skills
Evals
Files

types-unions-intersections.mdreferences/

Union and Intersection Types

Union Types

Represent values that can be one of several types:

type Result = Success | Failure;
type ID = string | number;
type Status = 'pending' | 'approved' | 'rejected';

function handleResult(result: Result) {
  // Type narrowing required to access specific properties
  if ('data' in result) {
    console.log(result.data);
  }
}

Intersection Types

Combine multiple types into one:

type Named = { name: string };
type Aged = { age: number };
type Person = Named & Aged;

const person: Person = {
  name: 'Alice',
  age: 30
};

// Useful for mixins
type Timestamped = { createdAt: Date; updatedAt: Date };
type User = Person & Timestamped;

Discriminated Unions

Union types with a common discriminant property for type-safe narrowing:

type Success = {
  kind: 'success';
  data: string;
};

type Failure = {
  kind: 'failure';
  error: Error;
};

type Result = Success | Failure;

function handleResult(result: Result) {
  switch (result.kind) {
    case 'success':
      console.log(result.data); // TypeScript knows this is Success
      break;
    case 'failure':
      console.error(result.error); // TypeScript knows this is Failure
      break;
  }
}

State Machines with Discriminated Unions

Model state machines type-safely:

type IdleState = { status: 'idle' };
type LoadingState = { status: 'loading'; startedAt: Date };
type SuccessState = { status: 'success'; data: string };
type ErrorState = { status: 'error'; error: Error };

type State = IdleState | LoadingState | SuccessState | ErrorState;

function transition(state: State): State {
  switch (state.status) {
    case 'idle':
      return { status: 'loading', startedAt: new Date() };
    case 'loading':
      // Can access startedAt here
      return Math.random() > 0.5
        ? { status: 'success', data: 'result' }
        : { status: 'error', error: new Error('Failed') };
    case 'success':
    case 'error':
      return { status: 'idle' };
  }
}

Intersection vs Extension

// Intersection - combines types
type A = { a: string };
type B = { b: number };
type C = A & B; // { a: string; b: number }

// Extension - interface inheritance
interface D extends A, B {
  c: boolean;
}

// Intersection allows for more flexible composition
type Mixed = A & { c: boolean } & B;

Best Practices

  1. Use discriminated unions for mutually exclusive states
  2. Prefer union types for OR relationships
  3. Use intersection types for AND relationships
  4. Add exhaustiveness checks with never
  5. Use literal types in discriminants

Common Patterns

Optional Properties

type Config = {
  required: string;
} & (
  | { mode: 'development' }
  | { mode: 'production'; apiKey: string }
);

Branded Types with Intersection

type Brand<T, B> = T & { __brand: B };
type UserId = Brand<string, 'UserId'>;
type Email = Brand<string, 'Email'>;

// UserId and Email are incompatible despite both being strings

Anti-Patterns

  1. Don't use unions when discriminated unions are appropriate
  2. Avoid deep intersection nesting
  3. Don't forget exhaustiveness checks
  4. Avoid conflicts in intersection types

Install with Tessl CLI

npx tessl i pantheon-ai/typescript-advanced@0.1.1

references

compiler-module-resolution.md

compiler-performance.md

compiler-strict-mode.md

compiler-tsconfig.md

docs-adr-templates.md

docs-framework-docs.md

docs-jsdoc-patterns.md

docs-typedoc-config.md

guards-assertion-functions.md

guards-basic.md

guards-branded-types.md

guards-discriminated-unions.md

guards-exhaustiveness.md

guards-generic.md

guards-inference-infer.md

guards-inference-return.md

patterns-advanced-generics.md

patterns-api-client.md

patterns-branded-types.md

patterns-builder.md

patterns-deep-readonly.md

patterns-dependency-injection.md

patterns-event-emitter.md

patterns-form-validation.md

patterns-plugin-system.md

patterns-recursive-types.md

patterns-state-machine.md

patterns-type-safe-module.md

practices-illegal-states.md

practices-module-patterns.md

practices-runtime-validation.md

practices-type-first.md

types-conditional.md

types-generics.md

types-index-signatures.md

types-mapped.md

types-narrowing.md

types-template-literals.md

types-type-assertions.md

types-unions-intersections.md

utilities-custom-mapped-types.md

utilities-extract-exclude.md

utilities-key-remapping.md

utilities-nonnullable-awaited.md

utilities-partial-required.md

utilities-pick-omit.md

utilities-readonly-record.md

utilities-returntype-parameters.md

SKILL.md

tile.json