-
Notifications
You must be signed in to change notification settings - Fork 539
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Description
Adding a type discriminator such as an ok: boolean property to result types such as PostgrestResponseSuccess would make them discriminated unions. This has a number of advantages. You can use if (!res.ok) { ... } to do type narrowing. It also lets the type checker ensure that success and error are mutually exclusive, and there is not some third state of { data: null, error: null }.
Suggested solution
For example,
| export interface PostgrestResponseSuccess<T> extends PostgrestResponseBase { |
export interface PostgrestResponseSuccess<T> extends PostgrestResponseBase {
ok: true
error: null
data: T
count: number | null
}
export interface PostgrestResponseFailure extends PostgrestResponseBase {
ok: false
error: PostgrestError
data: null
count: null
}
export type PostgrestSingleResponse<T> = PostgrestResponseSuccess<T> | PostgrestResponseFailureAlternative
No response
Additional context
Old Typescript handbook doc on discriminating unions https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#discriminating-unions
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request