A
arktype•12mo ago
Micha

How to include an external class into an arktype type?

I tried
import { TimeStub } from 'fauna'; // class

const clientDocument = type({
'id?': 'string',
'coll?': 'string',
'ts?': TimeStub
});
import { TimeStub } from 'fauna'; // class

const clientDocument = type({
'id?': 'string',
'coll?': 'string',
'ts?': TimeStub
});
and
import { TimeStub } from 'fauna'; // class

const clientDocument = type({
'id?': 'string',
'coll?': 'string',
'ts?': ['instanceof', TimeStub]
});
import { TimeStub } from 'fauna'; // class

const clientDocument = type({
'id?': 'string',
'coll?': 'string',
'ts?': ['instanceof', TimeStub]
});
but both create a Type<never> for me. I'm using arktype 1.0.29-alpha
19 Replies
ssalbdivad
ssalbdivad•12mo ago
The second should work, are you able to repro it with the class definition or some minimal case?
Micha
MichaOP•12mo ago
Not exactly sure what you mean, but this is the "TimeStub" class:
export declare class TimeStub {
readonly isoString: string;
/**
* @remarks constructor is private to enforce using factory functions
*/
private constructor();
/**
* Creates a new {@link TimeStub} from an ISO date string
* @param isoString - An ISO date string.
* @returns A new {@link TimeStub}
* @throws TypeError if a string is not provided, or RangeError if item
* is not a valid date
*/
static from(isoString: string): TimeStub;
/**
* Creates a new {@link TimeStub} from a Javascript `Date`
* @param date - A Javascript `Date`
* @returns A new {@link TimeStub}
*/
static fromDate(date: Date): TimeStub;
/**
* Get a copy of the `TimeStub` converted to a Javascript `Date`. Does not
* mutate the existing `TimeStub` value.
* @returns A `Date`
*/
toDate(): Date;
/**
* Override default string conversion
* @returns the string representation of a `TimeStub`
*/
toString(): string;
}
export declare class TimeStub {
readonly isoString: string;
/**
* @remarks constructor is private to enforce using factory functions
*/
private constructor();
/**
* Creates a new {@link TimeStub} from an ISO date string
* @param isoString - An ISO date string.
* @returns A new {@link TimeStub}
* @throws TypeError if a string is not provided, or RangeError if item
* is not a valid date
*/
static from(isoString: string): TimeStub;
/**
* Creates a new {@link TimeStub} from a Javascript `Date`
* @param date - A Javascript `Date`
* @returns A new {@link TimeStub}
*/
static fromDate(date: Date): TimeStub;
/**
* Get a copy of the `TimeStub` converted to a Javascript `Date`. Does not
* mutate the existing `TimeStub` value.
* @returns A `Date`
*/
toDate(): Date;
/**
* Override default string conversion
* @returns the string representation of a `TimeStub`
*/
toString(): string;
}
And if I try to inlcude it into type() it has the Type<never> type (Screenshot attached)
export declare class TimeStub {
readonly isoString: string;
/**
* @remarks constructor is private to enforce using factory functions
*/
private constructor();
/**
* Creates a new {@link TimeStub} from an ISO date string
* @param isoString - An ISO date string.
* @returns A new {@link TimeStub}
* @throws TypeError if a string is not provided, or RangeError if item
* is not a valid date
*/
static from(isoString: string): TimeStub;
/**
* Creates a new {@link TimeStub} from a Javascript `Date`
* @param date - A Javascript `Date`
* @returns A new {@link TimeStub}
*/
static fromDate(date: Date): TimeStub;
/**
* Get a copy of the `TimeStub` converted to a Javascript `Date`. Does not
* mutate the existing `TimeStub` value.
* @returns A `Date`
*/
toDate(): Date;
/**
* Override default string conversion
* @returns the string representation of a `TimeStub`
*/
toString(): string;
}

const clientDocument = type({
'id?': 'string',
'coll?': 'string',
'ts?': ['instanceof', TimeStub]
});
export declare class TimeStub {
readonly isoString: string;
/**
* @remarks constructor is private to enforce using factory functions
*/
private constructor();
/**
* Creates a new {@link TimeStub} from an ISO date string
* @param isoString - An ISO date string.
* @returns A new {@link TimeStub}
* @throws TypeError if a string is not provided, or RangeError if item
* is not a valid date
*/
static from(isoString: string): TimeStub;
/**
* Creates a new {@link TimeStub} from a Javascript `Date`
* @param date - A Javascript `Date`
* @returns A new {@link TimeStub}
*/
static fromDate(date: Date): TimeStub;
/**
* Get a copy of the `TimeStub` converted to a Javascript `Date`. Does not
* mutate the existing `TimeStub` value.
* @returns A `Date`
*/
toDate(): Date;
/**
* Override default string conversion
* @returns the string representation of a `TimeStub`
*/
toString(): string;
}

const clientDocument = type({
'id?': 'string',
'coll?': 'string',
'ts?': ['instanceof', TimeStub]
});
No description
ssalbdivad
ssalbdivad•12mo ago
I would guess the problem is the private constructor Sorry wrong image one sec
ssalbdivad
ssalbdivad•12mo ago
No description
ssalbdivad
ssalbdivad•12mo ago
You can use a cast to get around it like this:
export const myType = type({
'id?': 'string',
'coll?': 'string',
'ts?': ['instanceof', TimeStub] as Infer<TimeStub>
})
export const myType = type({
'id?': 'string',
'coll?': 'string',
'ts?': ['instanceof', TimeStub] as Infer<TimeStub>
})
Micha
MichaOP•12mo ago
With that is shows me ts as undefined
No description
ssalbdivad
ssalbdivad•12mo ago
That's weird I just tested that with your example and it worked
Micha
MichaOP•12mo ago
Are you tested it with 2.0.0? Because I'm on 1.0.29-alpha
ssalbdivad
ssalbdivad•12mo ago
I was looking at .27-alpha, is strict enabled?
Micha
MichaOP•12mo ago
you mean in tsconfig.json? Yes
ssalbdivad
ssalbdivad•12mo ago
https://stackblitz.com/edit/rzkceh-2zawsh?file=demo.ts Maybe it's a TS version thing? Can you repro it here?
David Blass
StackBlitz
Fayxym (forked) - StackBlitz
ArkType demo demo
ssalbdivad
ssalbdivad•12mo ago
I tested with 5.0 and 5.4 it seems like it works on both
Micha
MichaOP•12mo ago
On your stackblitz I see it working - that's good - now I need to find out what's wrong with my machine/setup 😉
ssalbdivad
ssalbdivad•12mo ago
Yeah that's very bizarre I have no idea what it would be
Micha
MichaOP•12mo ago
I'm still not sure why the undefined happens, but I was able to create a reproduction for this: https://github.com/arktypeio/arktype/issues/916
GitHub
Type ['instanceof', TimeStub] as Infer is undefined · Issue #916 · ...
Report a bug @ssalbdivad I have created a reproduction of yesterday's undefined problem Discord Discussion. 🧩 Context ArkType version: 1.0.29-alpha TypeScript version (4.8+): 5.4.2 Other contex...
ssalbdivad
ssalbdivad•12mo ago
Thanks for the repro, just added a comment! Should be an easy one to resolve by changing the .d.ts file to a .ts file.
Micha
MichaOP•9mo ago
Has the way changed in v2 of getting the Arktype of a TS class with a private constructor? @ssalbdivad type(['instanceof', TimeStub] as Infer<TimeStub>); is not more working for me. It tells me, Cannot find name "Infer". Did you mean "NoInfer"?
Dimava
Dimava•9mo ago
I think you can
type(['instanceof', TimeStub as new()=>TimeStub])
type(['instanceof', TimeStub as new()=>TimeStub])
Anyways, now it's type.cast<>
No description
ssalbdivad
ssalbdivad•9mo ago
Yes I have this in my unit tests now 😊
No description

Did you find this page helpful?