Stronger application-layer types remove impossible states
DEV Community

Stronger application-layer types remove impossible states

Stronger application-layer types remove impossible states

Today I completed a small Company write vertical slice in TypeScript, from business validation to PostgreSQL persistence. One detail clarified why application services are more than repository wrappers.

The repository update returns Company | undefined. That makes sense at the data layer: the query can succeed while the requested row does not exist. But the application service translates undefined into an explicit not-found result.

After that translation, keeping Company | undefined in the successful return type would force every caller to handle a state the service has made impossible. So the application contract returns either:

  • success with a Company
  • validation failure
  • not-found failure
  • repository failure

The type now describes the use-case guarantee instead of leaking the repository's lower-level uncertainty.

The practical lesson: good TypeScript types do more than prevent syntax mistakes. They help make invalid or impossible application states unrepresentable.

Comments

No comments yet. Start the discussion.