Types utilitaires

Types TypeScript génériques pour construire des plugins robustes.

Prettify<T>

Rend les types complexes lisibles dans les tooltips IDE.

type Prettify<T> = { [K in keyof T]: T[K] } & {};

type MyType = Prettify<PluginContext & { extra: string }>;
// Hover shows flat object instead of intersection type

DeepReadonly<T>

Rend un objet immuable récursivement.

type Config = DeepReadonly<{
  db: { url: string; pool: number };
}>;

const config: Config = { db: { url: "...", pool: 10 } };
config.db.pool = 20; // TypeScript error

NonEmptyArray<T>

Tableau garantissant au moins un élément.

type NonEmptyArray<T> = [T, ...T[]];

function first<T>(arr: NonEmptyArray<T>): T {
  return arr[0]; // Safe, no undefined
}

AsyncResult<T, E>

Version asynchrone du type Result.

type AsyncResult<T, E = string> = Promise<Result<T, E>>;

async function fetchUser(id: string): AsyncResult<User> {
  const user = await db.user.findUnique({ where: { id } });
  if (!user) return { success: false, error: "User not found" };
  return { success: true, data: user };
}

Previous

← Page précédente

Next

Page suivante →