ts-macros
ts-macros copied to clipboard
[BUG] `Save<...>` changes result type
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.
Interesting, thanks for the bug report! I'm not sure how that's even possible...