ts-macros icon indicating copy to clipboard operation
ts-macros copied to clipboard

[BUG] `Save<...>` changes result type

Open rijenkii opened this issue 1 year ago • 1 comments

Describe the bug When adding Save<> around an argument type, return type changes for some reason.

Code to reproduce

abstract class _Result<T, E> {
  public value: T | E;
}
class Ok<T> extends _Result<T, never> {
  constructor(public value: T) {
    super();
  }
}
class Err<E> extends _Result<never, E> {
  constructor(public value: E) {
    super();
  }
}
type Result<T, E> = Ok<T> | Err<E>;

function $try<T, E>(result: Save<Result<T, E>>): T {
  $$escape!(() => {
    if (result instanceof Err) {
      return result;
    }
  });
  return result.value as T;
}

declare const result: Result<null, string>;
const z = $try!(result);
//    ^? z: string

Expected behavior Expected type of z to be null.

Additional context After removing Save from type of $try.result, type of z becomes null, as expected.

rijenkii avatar Feb 07 '24 07:02 rijenkii

Interesting, thanks for the bug report! I'm not sure how that's even possible...

GoogleFeud avatar Feb 07 '24 20:02 GoogleFeud