Exercício prático · TypeScript

Practice lab: Validate a boundary and model its states

do caderno de estudos · atualizado em 2 de outubro de 2026
Para quem vai encarar entrevista de TypeScript2 min

Tente resolver a tarefa antes de consultar os critérios. Compare o resultado e explique o que mudou no seu raciocínio.

Tarefa e critérios de verificação

Task, 15 minutes: define a User with a non-empty string id and a finite, non-negative integer age. Write parseUser(value: unknown) that returns a validated User or reports an error. Do not use as User as validation. Model the consumer as a discriminated union with loading, success, and error states.

Check your work:

  • { id: "u1", age: 42 } is accepted. null, an array, a missing id, a numeric id, a string age, and NaN are rejected.
  • The success branch can access data. The error branch can access its error. The loading branch cannot access either.
  • Add a cancelled state. An exhaustive switch should fail type checking until that state is handled.

Follow-up: explain why Partial<User> neither validates a PATCH request nor specifies which fields the caller is allowed to change.

No app, a tarefa fica visível e você pode revelar os critérios depois da tentativa. Use as notas para registrar sua solução.

Praticar este exercício no app