Explain TypeScript code
Paste TypeScript, generics and all, and Vibe explains what the code does and what its types are telling you.
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>andgetJson<T>work for any shape of data.getJson<User>fills inTasUser, soresult.valueis typed as aUser.- type narrowing
Resultis a union of two object types told apart byok. Afterif (!result.ok) return ..., TypeScript knows only the{ ok: true; value: T }case is left, so readingresult.valuecompiles.- async/await
- Both functions are
async, so they return aPromise.awaitwaits for the response, and then for its body, before the next line runs. - modules
exportmakes the two functions importable from other files.ResultandUserare not exported, so they stay private to this file.
Good to know
as Tis 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?: stringmeans the field may be missing, so its type isstring | undefined. The ternary inuserLabelhandles both cases.- With
stricton, acatchvariable has the typeunknown, so the code checkserr instanceof Errorbefore readingerr.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).