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

guards-generic.mdreferences/

Generic Type Guards

Overview

Generic type guards provide reusable, type-safe narrowing functions that work across different types.

Basic Generic Type Predicate

function isArray<T>(value: T | T[]): value is T[] {
  return Array.isArray(value);
}

const value: string | string[] = getSomeValue();
if (isArray(value)) {
  // value is narrowed to string[]
  value.map(s => s.toUpperCase());
}

Generic Property Check

function hasProperty<T, K extends string>(
  obj: T,
  key: K
): obj is T & Record<K, unknown> {
  return typeof obj === "object" && obj !== null && key in obj;
}

const data: unknown = { name: "Alice", age: 30 };
if (hasProperty(data, "name")) {
  // data is narrowed to have name property
  console.log(data.name);
}

Generic Instance Check

function isInstance<T>(
  value: unknown,
  constructor: new (...args: any[]) => T
): value is T {
  return value instanceof constructor;
}

const val: unknown = new Date();
if (isInstance(val, Date)) {
  // val is narrowed to Date
  console.log(val.getTime());
}

Generic Array Element Check

function isArrayOf<T>(
  value: unknown,
  guard: (item: unknown) => item is T
): value is T[] {
  return Array.isArray(value) && value.every(guard);
}

function isString(value: unknown): value is string {
  return typeof value === "string";
}

const data: unknown = ["a", "b", "c"];
if (isArrayOf(data, isString)) {
  // data is narrowed to string[]
  data.forEach(s => console.log(s.toUpperCase()));
}

Generic Optional Value Check

function isDefined<T>(value: T | undefined | null): value is T {
  return value !== undefined && value !== null;
}

const values = [1, undefined, 2, null, 3];
const defined = values.filter(isDefined); // number[]

Generic Type Match

type TypeMap = {
  string: string;
  number: number;
  boolean: boolean;
};

function isType<K extends keyof TypeMap>(
  value: unknown,
  type: K
): value is TypeMap[K] {
  return typeof value === type;
}

const val: unknown = "hello";
if (isType(val, "string")) {
  // val is narrowed to string
  console.log(val.toUpperCase());
}

Best Practices

  • Use descriptive generic parameter names
  • Constrain generics when possible for better type safety
  • Combine generic guards for complex validation
  • Consider performance for hot paths
  • Document expected guard behavior

Common Pitfalls

  • Over-constraining generics limits reusability
  • Forgetting to check for null/undefined in object guards
  • Runtime checks must match type predicate guarantees
  • Generic guards can't narrow discriminated unions directly

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