Hacker News new | past | comments | ask | show | jobs | submit login

I do it the "go" way, with:

  type Result<K = any, T extends Error = Error> = [K, null] | [null, T];
And use it:

  const [response, error] = await tryFetch<Type>("");
  if (error) { handle(); }



But this way you are losing the type checker yelling at you if you try and use response without having handled error. What I like about

    res = something(): Thing | Error
is I have to do

    if (res instanceof Error) {
      handle()
      return res
    }

    doSomethingWith(res)
or else the compiler will yell at me for trying to doSomethingWith(Error)


I see. Well, for what it's worth, in my tuple example, you either have one or the other, i. e. the K result or an Error; so, if you don't check for error (or result), the compiler gives an error saying that "K is possibly `null`".

Example: https://tsplay.dev/w2p0Vm


that makes sense, and I like the simplicity of `if (err)`. I'm sure it's uncommon but you still lose the compiler check if the function can return null as a matter of course.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: