Type 'distillOut<T>' is not assignable to type 'T'.

import { type, Type } from "arktype";

const test = <T>(validator: Type<T>): T => {
  const result = validator({});
  if (result instanceof type.errors) throw new Error();
  return result;
}

This produces error:
Type 'distillOut<T>' is not assignable to type 'T'.
  'T' could be instantiated with an arbitrary type which could be unrelated to 'distillOut<T>'.

If I cast, then it works:
import { type, Type } from "arktype";

const test = <T>(validator: Type<T>): T => {
  const result = validator({});
  if (result instanceof type.errors) throw new Error();
  return result as T;
}

Why is the casting necessary? Shouldn't it be type T after checking it's not type.errors?
Was this page helpful?