Complete worked example
typescript
type Role = { id: string; title: string };
function decodeRole(raw: unknown): Role | undefined {
if(typeof raw !== "object" || raw === null || !("id" in raw) || typeof raw.id !== "string" || !raw.id
|| !("title" in raw) || typeof raw.title !== "string") return undefined;
return {id:raw.id,title:raw.title};
}- Reasoning
- Only the fresh projected result is trusted after all required field checks pass.