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;
}
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>'.
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;
}
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?
18 Replies
Dimava
Dimava4mo ago
T may be (v: Foo) => Bar i.e. a morph Also check Type<Foo>.allows, it doesn't morph iirc
TizzySaurus
TizzySaurus4mo ago
It should also be : Type<T>["infer"] iirc. Type<T> doesn't validate to type T because of constraints
Dimava
Dimava4mo ago
Type<T>['infer']
SynthLuvr
SynthLuvr4mo ago
But if I do do a morph, shouldn't that be captured by T? Example:
test(type("string").pipe((s) => parseInt(s)));
test(type("string").pipe((s) => parseInt(s)));
In this case, shouldn't it evaluate to:
test<Number>(...);
test<Number>(...);
?
Dimava
Dimava4mo ago
I would say, just don't do the return type Let it infer itself Also, isn't your function just type().assert?
SynthLuvr
SynthLuvr4mo ago
Ok yes this works
Dimava
Dimava4mo ago
Or you remap the error?
Dimava
Dimava4mo ago
@SynthLuvr
No description
Dimava
Dimava4mo ago
@SynthLuvr
No description
TizzySaurus
TizzySaurus4mo ago
@SynthLuvr since you don't seem to be aware, t.assert(...) is equivalent to
function assert(t: Type) {
const out = t(...)
if (out instanceof type.errors) throw new Error(out.summary)
return out
}
function assert(t: Type) {
const out = t(...)
if (out instanceof type.errors) throw new Error(out.summary)
return out
}
Err except I think it throws AggregateError instead of Error
SynthLuvr
SynthLuvr4mo ago
I think that's unrelated and off topic though Will try with this
TizzySaurus
TizzySaurus4mo ago
The point is that your function seems to just be duplicating the built-in assert that already exists
Dimava
Dimava4mo ago
Okay so, Bacause this
Dimava
Dimava4mo ago
No description
SynthLuvr
SynthLuvr4mo ago
Ok but that's still off topic. I'm specifically asking about the type, not about about built-in functions. Appreciate the help though
Want results from more Discord servers?
Add your server