remove custom Result type from @inlang/core/query #262
-
ProblemThe custom Result type is non-standard, not-serializable, and thus leads to more problems than it solves?
Both problems could be solved. But more problems emerged:
Alternatives
Additional remarksInteresting how other APIs deal with this issue (Firebase, Supabase, etc.) |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 11 replies
-
Leaning toward the removal of Result. The result behavior is unusual, does not mix nicely with other libraries that expect a throw, and seems to add too little to negative benefit. Supabase changed to a result-based API supabase/supabase-js#32. Some comments resemble the DX problems of using such API in libraries like SWR. Other users have followed with the request to have an |
Beta Was this translation helpful? Give feedback.
-
A type-only type Result<T, E> = { data: T, error?: undefined } | { data?: undefined, error: E }
function x(): Result<string, "something wen't wrong"> {
return { data: "hello" }
}
const { data, error } = x()
console.log(data)
We could provide additional utilities like // will throw if an error has been returned.
const data = unwrap(x()) |
Beta Was this translation helpful? Give feedback.
-
@ivanhofer I vote for a simple union type that optimizes for DX of consumption. Two proposals: Proposal A object syntax
type ResultA<T, E> = { data: T, error?: undefined } | { data?: undefined, error: E }
function getRefernceMessageA(): ResultA<string, "something went wrong"> {
return { error: "something went wrong"}
}
function getMessageA(): ResultA<string, "something went wrong"> {
return { data: "Willkommen zu dieser App." }
}
// ------------------------------------------------------------------
// consumption is annoying and requires an unwrap. using two results in one
// code block will always lead to re-declarations of the return values.
const { data, error } = getRefernceMessageA()
const { data, error } = getMessageA()
// -------------------------------------------------------------------
const referenceMessageA = getRefernceMessageA()
const targetMessageA = getMessageA()
if (referenceMessageA.error || targetMessageA.error){
throw Error("Query went wrong")
}
// both values are defined now
referenceMessageA.data
targetMessageA.data Proposal B tuple syntax
type ResultB<T, E> = [data: T, error?: undefined ] | [ data: undefined, error: E ]
function getRefernceMessageB(): ResultB<string, "something went wrong"> {
return [, "something went wrong"]
}
function getMessageB(): ResultB<string, "something went wrong"> {
return ["Willkommen zu dieser App."]
}
const [ referenceMessage, errorReference ] = getRefernceMessageB()
const [ targetMessage, errorTarget ] = getMessageB()
if (errorReference || errorTarget){
throw Error("Query went wrong.")
}
// both types are defined now
referenceMessage
targetMessage |
Beta Was this translation helpful? Give feedback.
-
Implemented tuple based result. |
Beta Was this translation helpful? Give feedback.
Implemented tuple based result.