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

patterns-branded-types.mdreferences/

Branded Types (Nominal Typing)

Create distinct types from primitives to prevent mixing incompatible values.

Basic Branded Type

type Brand<K, T> = K & { __brand: T };

type UserId = Brand<string, 'UserId'>;
type ProductId = Brand<string, 'ProductId'>;

// Factory functions
const createUserId = (id: string): UserId => id as UserId;
const createProductId = (id: string): ProductId => id as ProductId;

const userId = createUserId('user-123');
const productId = createProductId('prod-456');

// Type error: can't mix branded types
// getUser(productId); // Error!
function getUser(id: UserId) {
  return { id, name: 'Alice' };
}

Validated Branded Types

type Email = Brand<string, 'Email'>;
type PhoneNumber = Brand<string, 'PhoneNumber'>;

const createEmail = (value: string): Email => {
  if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) {
    throw new Error('Invalid email');
  }
  return value as Email;
};

const createPhoneNumber = (value: string): PhoneNumber => {
  if (!/^\+?[\d\s-()]+$/.test(value)) {
    throw new Error('Invalid phone number');
  }
  return value as PhoneNumber;
};

// Usage
const email = createEmail('user@example.com'); // OK
// const invalid = createEmail('not-an-email'); // Throws

Numeric Branded Types

type PositiveInt = Brand<number, 'PositiveInt'>;
type Percentage = Brand<number, 'Percentage'>;

const createPositiveInt = (n: number): PositiveInt => {
  if (!Number.isInteger(n) || n <= 0) {
    throw new Error('Must be a positive integer');
  }
  return n as PositiveInt;
};

const createPercentage = (n: number): Percentage => {
  if (n < 0 || n > 100) {
    throw new Error('Must be between 0 and 100');
  }
  return n as Percentage;
};

Branded Types with Zod

import { z } from 'zod';

const UserIdSchema = z.string().uuid().brand('UserId');
type UserId = z.infer<typeof UserIdSchema>;

const EmailSchema = z.string().email().brand('Email');
type Email = z.infer<typeof EmailSchema>;

// Validation
const userId = UserIdSchema.parse('550e8400-e29b-41d4-a716-446655440000');
const email = EmailSchema.parse('user@example.com');

Use Cases

  1. Prevent ID mixing: UserId vs ProductId
  2. Domain constraints: PositiveInt, NonEmptyString
  3. Validated formats: Email, URL, PhoneNumber
  4. Units: Meters, Seconds, Dollars
  5. Security: SanitizedHtml, HashedPassword

Install with Tessl CLI

npx tessl i pantheon-ai/typescript-advanced

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