How to properly make a property of a class type?
I tried this:
but
typeof params.infer
didn't bring the correct type, instead it looks like:
i.e. like some properties40 Replies
Is this related?
https://github.com/arktypeio/arktype/issues/760
GitHub
Ensure constructor expressions aren't treated as morphs ยท Issue #76...
declare function isClass(input: unknown): input is new (...args: any[]) => any; const Class = type(["Function", "=>", isClass]); // ^ correctly inferred as Type<new (.....
Try
as Infer<TokenManager>
Sorry, where should I put it?
Yeah you were right about the issue you linked, it is.... fixed on beta....
If you cast a definition like that it will cause it to be not validated and inferred as you wrote
It should still validate the properties from your class though either way it just pereserves the name on beta but not alpha
This is not clear enough for me, sorry ๐
Could you just provide an example of how it can be done?
Unknown Userโข14mo ago
Message Not Public
Sign In & Join Server To View
it should be something else I believe
because
tokenManager
is of the type TokenManager
already according to the type
the fix should come somewhere welseUnknown Userโข14mo ago
Message Not Public
Sign In & Join Server To View
I think this might be one of the cases where since the properties are extracted out in the final step, on alpha it doesn't matter even if you do cast
But I might be able to publish a quick release to port the fix from beta over ๐ซ
Thank you, that would be great @ssalbdivad
What about the release date of beta?
Is it way not ready yet or maybe we can switch to it already? ๐
No not sure yet will finish as soon as I can
Once the tests pass I will publish a dev version
You can try
1.0.23-alpha
@Dimava You might also be interested in this version since the fix was your suggestion to preserve names when inferring from an external classI don't really use it anywhere so I'll wait for ArkClass
@ssalbdivad I tried 1.0.23. Unfortunately it's still not validating: https://stackblitz.com/edit/typescript-whskrs?file=index.ts
That's because of using the
#state
private prop I believe
Here is another sandbox where I replaced #state
with private _state
: https://stackblitz.com/edit/typescript-ghgxte?file=index.ts
It didn't resolve the issue tho
Making it public resovles the issue. However...
Here is one more sandbox, where I removed all privates but added a method: https://stackblitz.com/edit/typescript-pmp9en?file=index.ts
Also not validatingWith a private prop like this it is impossible unless either the class is pre-registered in some kind of merged interface. You can do something like this though:
"the class is pre-registered in some kind of merged interface" - what do you mean here, sorry?
There's a workaround potentially involving interface merging where I could check for that class specifically and not recurse into it, but it isn't implemented yet
What is the danger of this approach and why
foo.infer
doesn't use it instead?
I mean typeof foo extends Type<infer t> ? t : never;
Because it requires you to explicitly "register" your class ahead of time
Because it could include morphs
That's what the point of
.infer
and .inferIn
are, to extract morphsMeh, I cannot grasp it this fast. Because I basically don't know what is morph
register a class ahead of time - is like... delcaring it?
Is that what I do here or not? https://stackblitz.com/edit/typescript-mq5rax?file=index.ts
There's kind of a lot of layers here just trust me for now there is not a workaround to get a nominal representation of a class with a private member other than to explicitly infer the type separately
I see, ok
I believe you!
I just try to undersstand what exactly I'm doing with
Type<infer t>
and should I do this now like alwaysYou only ever need to do that if you have a class with private members
Do you mean
#aPrivate
or private aPrivate
?Either one TS treats them the same
Ok, and what about the last sandbox where I just use a public method?
The core problem is if you map over them, that prop is omitted
That should work fine now
I hope
Method turned into
someAction: void;
Mmm maybe you're right there was another problem on alpha with how morphs were inferred
Brutal
Ok. Let's just wait for beta
please don't distract on alpha โค๏ธ
Can't wait to see the full power of arktype when it reaches production
Maybe I will build the
interface
thing I mentioned which could be a workaround on alpha that would also be useful in beta(Also in my particular case I think about kicking the class out of for the interfaces and just pass it as a separate parameter)
(also I'm pretty fine with
as
-assertions for now)
๐Haha well whatever works in the mean time ๐ Basically the
interface
strategy would allow you to specify a union of types not to recurse into, so e.g. you could write something like
Or similar and then you could infer those classes directly=How to make it
|undefined
?
I mean this:
I don't want to make it 'tokenManager?:' ...
as it's breaking something else
I decided to return it back to my config
and not to pass as a separate parameter
because it also needs validationUnknown Userโข14mo ago
Message Not Public
Sign In & Join Server To View
Ah! Let me try
It worked btw ๐