A
arktype3w ago
Micha

What is the arktype 2 syntax for inferring classes with a private constructor?

In Arktype 1.x I was writing:
import { type, Infer } from 'arktype';

const document = type({
"ttl?": ["instanceof", TimeStub] as Infer<TimeStub>,
})
import { type, Infer } from 'arktype';

const document = type({
"ttl?": ["instanceof", TimeStub] as Infer<TimeStub>,
})
But the Infer import is not more available in arktype 2, so I'm wondering how to achieve it now? Maybe the answer could be also added to the docs 🙂 https://arktype.io/docs/objects#instanceof
5 Replies
Micha
MichaOP3w ago
Seems I'm getting a bit closer:
const document = type({
"ttl?": type.instanceOf(TimeStub) as type.infer<TimeStub>,
});
const document = type({
"ttl?": type.instanceOf(TimeStub) as type.infer<TimeStub>,
});
But this throws a conversion error:
Conversion of type 'Type<TimeStub, {}>' to type '{ isoString: never; toDate: {}; toString: {}; }' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Type 'Type<TimeStub, {}>' is missing the following properties from type '{ isoString: never; toDate: {}; toString: {}; }': isoString, toDatets(2352)
Conversion of type 'Type<TimeStub, {}>' to type '{ isoString: never; toDate: {}; toString: {}; }' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Type 'Type<TimeStub, {}>' is missing the following properties from type '{ isoString: never; toDate: {}; toString: {}; }': isoString, toDatets(2352)
If I cast it to unknown
export const document_update = type({
"ttl?": type.instanceOf(TimeStub) as unknown as type.infer<TimeStub>,
});
export const document_update = type({
"ttl?": type.instanceOf(TimeStub) as unknown as type.infer<TimeStub>,
});
then I get again back to the initial error:
Argument of type 'typeof TimeStub' is not assignable to parameter of type 'Constructor<TimeStub>'.
Cannot assign a 'private' constructor type to a 'public' constructor type.ts(2345)
Argument of type 'typeof TimeStub' is not assignable to parameter of type 'Constructor<TimeStub>'.
Cannot assign a 'private' constructor type to a 'public' constructor type.ts(2345)
Micha
MichaOP3w ago
Like this?
export const document_update = type({
"ttl?": TimeStub as type.cast<TimeStub>,
});
export const document_update = type({
"ttl?": TimeStub as type.cast<TimeStub>,
});
ssalbdivad
ssalbdivad3w ago
That would work
Micha
MichaOP3w ago
Awesome - you're a live saver! ❤️

Did you find this page helpful?