Skip to content

Instantly share code, notes, and snippets.

@turbod
Created July 17, 2023 06:51
Show Gist options
  • Save turbod/8158457fb5e0156c81a9ca2ffa9b8e4f to your computer and use it in GitHub Desktop.
Save turbod/8158457fb5e0156c81a9ca2ffa9b8e4f to your computer and use it in GitHub Desktop.
Api response different types based on status
type ApiResponse<T> =
| { status: 'success'; data: T; timestamp: Date }
| { status: 'error'; message: string; timestamp: Date }
const response1: ApiResponse<number> = {
status: 'success',
data: 100,
timestamp: new Date(),
}
const response2: ApiResponse<number> = {
status: 'success',
message: 'Something went wrong',
timestamp: new Date(),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment