Explain TypeScript code

Paste TypeScript, generics and all, and Vibe explains what the code does and what its types are telling you.

users.ts

Up to 1,500 characters (about 60 lines). Your code is sent to Unvibe and Google Gemini to write the explanation. Don't paste keys or passwords. Common key formats are refused.

Vibe24 lines selected

Press Explain it for the sample, or paste your own code first.

What this TypeScript sample shows

generics
Result<T> and getJson<T> work for any shape of data. getJson<User> fills in T as User, so result.value is typed as a User.
type narrowing
Result is a union of two object types told apart by ok. After if (!result.ok) return ..., TypeScript knows only the { ok: true; value: T } case is left, so reading result.value compiles.
async/await
Both functions are async, so they return a Promise. await waits for the response, and then for its body, before the next line runs.
modules
export makes the two functions importable from other files. Result and User are not exported, so they stay private to this file.

Good to know

  • as T is a promise to the compiler, not a check. If the server sends a different shape, nothing fails until a later line uses a missing field. Validate data that comes from outside your code.
  • email?: string means the field may be missing, so its type is string | undefined. The ternary in userLabel handles both cases.
  • With strict on, a catch variable has the type unknown, so the code checks err instanceof Error before reading err.message.

Watch out: getJson never throws: every failure becomes { ok: false }. A caller that reads .value without checking ok first gets a compile error, which is the point of the pattern.

Explain TypeScript code beside your editor

Unvibe is a free app for Mac and Windows. In the app there is no pasting: select code in any app and press Control+U (Ctrl+Alt+U on Windows).