News for hackers. Stuff that matters.
Re:Problems with TypeScript by eeperson on Wednesday January 15, 2020 @04:23PM (#22058785)
Do you mean:

    Failure { error: Error }
    Success<T> { value: T }
I assume you want value or error not value and error.
Re:Problems with TypeScript by valand on Wednesday January 15, 2020 @08:14PM (#22060736)
Limitation in TypeScript. Even in TS 3.6.x you can't do

```

if (result.error) { ... }

```

on a variable that possibly returns { value: T }

It'll complain that result may possibly have no attribute error

Re:Problems with TypeScript by paulddraper on Thursday January 16, 2020 @12:43AM (#22061982)
The discriminated union pattern is better [1]

    type Success<T> = { type: 'success', value: T};
    type Error = { type: 'error', value: string };
    type Result<T> = Success<T> | Error;

    if (result.type === 'error') {
      result.error;
    } else {
      result.value;
    }
[1] https://www.typescriptlang.org/docs/handbook/advanced-types....
Re:Problems with TypeScript by sondang on Wednesday January 15, 2020 @09:12PM (#22061071)
You can do:

  if ('error' in result) {
    console.error(result.error); // ok: result inferred as 'Failure' 
  } else {
    doThings(result.value) // ok: result inferred as 'Success<T>'
  }


Even though you used string there, it's pretty type-safe because if you have a typo in your string, the inferred types will propagate other type errors

  if ('errors' in result) { // typo
    console.error(result.errors); // Type error: result inferred as 'never' 
  } else {
    doThings(result.value) // Type error: result inferred as Success<T> | Failure
  }
Re:Problems with TypeScript by felipellrocha on Thursday January 16, 2020 @01:09AM (#22062070)
...

What do you expect here? The compiler is doing exactly what is expected.

Re:Problems with TypeScript by holtalanm on Wednesday January 15, 2020 @05:45PM (#22059654)
eh. the pattern he is trying to mimic from elixir is:

{:ok, value}

or

{:error, err}

so, you would be correct.