A
arktypeโ€ข15mo ago
OnkelTem

How to properly make a property of a class type?

I tried this:
class TokenManager {}
const params = type({
tokenManager: type(['instanceof', TokenManager]),
})
class TokenManager {}
const params = type({
tokenManager: type(['instanceof', TokenManager]),
})
but typeof params.infer didn't bring the correct type, instead it looks like:
tokenManager: {
refreshToken: Promise<TokenInfo>;
readonly isTokenValid: boolean;
readonly token: string | undefined;
};
tokenManager: {
refreshToken: Promise<TokenInfo>;
readonly isTokenValid: boolean;
readonly token: string | undefined;
};
i.e. like some properties
40 Replies
OnkelTem
OnkelTemOPโ€ข15mo ago
No description
OnkelTem
OnkelTemOPโ€ข15mo ago
No description
OnkelTem
OnkelTemOPโ€ข15mo ago
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 (.....
Dimava
Dimavaโ€ข15mo ago
Try as Infer<TokenManager>
OnkelTem
OnkelTemOPโ€ข15mo ago
Sorry, where should I put it?
ssalbdivad
ssalbdivadโ€ข15mo ago
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
OnkelTem
OnkelTemOPโ€ข15mo ago
This is not clear enough for me, sorry ๐Ÿ™‚ Could you just provide an example of how it can be done?
Unknown User
Unknown Userโ€ข15mo ago
Message Not Public
Sign In & Join Server To View
OnkelTem
OnkelTemOPโ€ข15mo ago
it should be something else I believe because tokenManager is of the type TokenManager already according to the type the fix should come somewhere welse
Unknown User
Unknown Userโ€ข15mo ago
Message Not Public
Sign In & Join Server To View
ssalbdivad
ssalbdivadโ€ข15mo ago
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 ๐Ÿซ 
OnkelTem
OnkelTemOPโ€ข15mo ago
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? ๐Ÿ™‚
ssalbdivad
ssalbdivadโ€ข15mo ago
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 class
Dimava
Dimavaโ€ข15mo ago
I don't really use it anywhere so I'll wait for ArkClass
OnkelTem
OnkelTemOPโ€ข15mo ago
@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 validating
ssalbdivad
ssalbdivadโ€ข15mo ago
With 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:
type Foo = typeof foo extends Type<infer t> ? t : never;
type Foo = typeof foo extends Type<infer t> ? t : never;
OnkelTem
OnkelTemOPโ€ข15mo ago
"the class is pre-registered in some kind of merged interface" - what do you mean here, sorry?
ssalbdivad
ssalbdivadโ€ข15mo ago
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
OnkelTem
OnkelTemOPโ€ข15mo ago
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;
ssalbdivad
ssalbdivadโ€ข15mo ago
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 morphs
OnkelTem
OnkelTemOPโ€ข15mo ago
Meh, 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
ssalbdivad
ssalbdivadโ€ข15mo ago
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
OnkelTem
OnkelTemOPโ€ข15mo ago
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 always
ssalbdivad
ssalbdivadโ€ข15mo ago
You only ever need to do that if you have a class with private members
OnkelTem
OnkelTemOPโ€ข15mo ago
Do you mean #aPrivate or private aPrivate?
ssalbdivad
ssalbdivadโ€ข15mo ago
Either one TS treats them the same
OnkelTem
OnkelTemOPโ€ข15mo ago
Ok, and what about the last sandbox where I just use a public method?
ssalbdivad
ssalbdivadโ€ข15mo ago
The core problem is if you map over them, that prop is omitted That should work fine now
ssalbdivad
ssalbdivadโ€ข15mo ago
I hope
OnkelTem
OnkelTemOPโ€ข15mo ago
Method turned into someAction: void;
ssalbdivad
ssalbdivadโ€ข15mo ago
Mmm maybe you're right there was another problem on alpha with how morphs were inferred Brutal
OnkelTem
OnkelTemOPโ€ข15mo ago
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
ssalbdivad
ssalbdivadโ€ข15mo ago
Maybe I will build the interface thing I mentioned which could be a workaround on alpha that would also be useful in beta
OnkelTem
OnkelTemOPโ€ข15mo ago
(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)
// TODO: ...
this.#tokenManager = tokenManager as unknown as TokenManager;
// TODO: ...
this.#tokenManager = tokenManager as unknown as TokenManager;
๐Ÿ™‚
ssalbdivad
ssalbdivadโ€ข15mo ago
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
interface ArkTypeConfig {
builtins: Foo | TokenManager
}
interface ArkTypeConfig {
builtins: Foo | TokenManager
}
Or similar and then you could infer those classes directly=
OnkelTem
OnkelTemOPโ€ข15mo ago
How to make it |undefined? I mean this:
const schema = type({
tokenManager: type(['instanceof', TokenManager])
})
const schema = type({
tokenManager: type(['instanceof', TokenManager])
})
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 validation
Unknown User
Unknown Userโ€ข15mo ago
Message Not Public
Sign In & Join Server To View
OnkelTem
OnkelTemOPโ€ข15mo ago
Ah! Let me try It worked btw ๐Ÿ™‚
Want results from more Discord servers?
Add your server