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
Dimava6mo ago
T may be (v: Foo) => Bar i.e. a morph Also check Type<Foo>.allows, it doesn't morph iirc
TizzySaurus
TizzySaurus6mo ago
It should also be : Type<T>["infer"] iirc. Type<T> doesn't validate to type T because of constraints
Dimava
Dimava6mo ago
Type<T>['infer']
SynthLuvr
SynthLuvrOP6mo 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
Dimava6mo ago
I would say, just don't do the return type Let it infer itself Also, isn't your function just type().assert?
SynthLuvr
SynthLuvrOP6mo ago
Ok yes this works
Dimava
Dimava6mo ago
Or you remap the error?
Dimava
Dimava6mo ago
@SynthLuvr
No description
Dimava
Dimava6mo ago
@SynthLuvr
No description
TizzySaurus
TizzySaurus6mo 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
SynthLuvrOP6mo ago
I think that's unrelated and off topic though Will try with this
TizzySaurus
TizzySaurus6mo ago
The point is that your function seems to just be duplicating the built-in assert that already exists
Dimava
Dimava6mo ago
Okay so, Bacause this
Dimava
Dimava6mo ago
No description
SynthLuvr
SynthLuvrOP6mo 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
Dimava
Dimava6mo ago
You're welcome to write the what usage example you want to achieve, maybe there's more builtin stuff
TizzySaurus
TizzySaurus6mo ago
If the behaviour already exists built-in then you don't need to worry about defining it yourself and figuring out the types. If you want to for practice or something then fine, but otherwise just use the built-ins There's a whole principal of not "reinventing the wheel" in programming
SynthLuvr
SynthLuvrOP6mo ago
This is what I needed. Thanks!
Want results from more Discord servers?
Add your server