A
arktype•14mo ago
OnkelTem

How to incorporate existing type?

I have a type imported from a third-party API and I'd like to build it in into my ark type:
import { Foo } from 'fancy-api'

const schema = type({
foo: // how to put Foo here?
})
import { Foo } from 'fancy-api'

const schema = type({
foo: // how to put Foo here?
})
51 Replies
OnkelTem
OnkelTemOP•14mo ago
I understand that validation will not be made in runtime but still, is there a nice way to add a stub for external types?
Unknown User
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
OnkelTem
OnkelTemOP•14mo ago
sure, it can only be an assertion isn't there a way to do it now?
Unknown User
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
ssalbdivad
ssalbdivad•14mo ago
So you just use a narrow function with a type guard or a cast
const schema = type({
foo: {bar: "string"} as Infer<Foo>
})

// or

const schema = type({
// use the narrow function to do extra validation
foo: [{bar: "string"}, "=>", (data): data is Foo => true ]
})
const schema = type({
foo: {bar: "string"} as Infer<Foo>
})

// or

const schema = type({
// use the narrow function to do extra validation
foo: [{bar: "string"}, "=>", (data): data is Foo => true ]
})
I think he was asking for how to use the type not how to implement the validator but yeah something similar to that could work
Unknown User
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
ssalbdivad
ssalbdivad•14mo ago
Well, that's casting for you
Unknown User
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
ssalbdivad
ssalbdivad•14mo ago
Just don't cast in TS KEKW good luck If you're using ArkType you're casting like a thousand times under the hood anyways you're just trusting me and I'm not that trustworthy
Unknown User
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
ssalbdivad
ssalbdivad•14mo ago
Probably impossible? It's literally impossible to write a generic function that returns a mapped or conditional type using its parameter without casting So given you can't write a function to replace a key in an object without casting, yeah I'd say ArkType is pretty doomed
Unknown User
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
ssalbdivad
ssalbdivad•14mo ago
Lmao you don't think I know DO YOU EVEN KNOW WHO I AM BOY
Unknown User
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Dimava
Dimava•14mo ago
import { Foo } from 'fancy-api'
import { type } from `[email protected]`

const foo = type.define<Foo>({ ... })
import { Foo } from 'fancy-api'
import { type } from `[email protected]`

const foo = type.define<Foo>({ ... })
OnkelTem
OnkelTemOP•14mo ago
You mean tha's how it will work in the future And 1.1 is what you folks usually call beta branch, right?
ssalbdivad
ssalbdivad•14mo ago
He just made that up but yeah that is what he means It's actually like this:
type Foo = {
a: string
}

declare<Foo>().type({
a: "string"
})
type Foo = {
a: string
}

declare<Foo>().type({
a: "string"
})
OnkelTem
OnkelTemOP•14mo ago
I see, cool I had to double cast in my code 🙂
OnkelTem
OnkelTemOP•14mo ago
First time here:
No description
OnkelTem
OnkelTemOP•14mo ago
And second time here:
No description
OnkelTem
OnkelTemOP•14mo ago
Because it seems like Infer did more with the original type than just "using" it
OnkelTem
OnkelTemOP•14mo ago
No description
OnkelTem
OnkelTemOP•14mo ago
I should say that PollyClientConfig definition is pretty... complex It's something like:
OnkelTem
OnkelTemOP•14mo ago
No description
OnkelTem
OnkelTemOP•14mo ago
Not sure if pollyConfig as PollyClientConfig is enough to make TS happy, i.e. to not make it trigger that instantiation deep error, so I added pollyConfig as unknown as PollyClientConfig
ssalbdivad
ssalbdivad•14mo ago
Huh I mean that does seem crazy. Have you tried using interfaces for this? They tend to be more performant for combining many objects
OnkelTem
OnkelTemOP•14mo ago
It's not my type I cannot change it
ssalbdivad
ssalbdivad•14mo ago
Yikes. Well this would be another case that would benefit significantly from the inferface boundary strategy I mentioned otherwise AT has to look through this type for morphs
Unknown User
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
ssalbdivad
ssalbdivad•14mo ago
I don't consider this to be a major API. The vast majority of the time the type will be inferred from the definition. The second option is impossible because that parameter would be for the definition. Maybe the first is fine, I think I probably considered it and thought it read more naturally if it was always type that actually instantiated the type
Dimava
Dimava•14mo ago
U need double generic that's why
ssalbdivad
ssalbdivad•14mo ago
Yeah the second is doomed I guess The first rather
Dimava
Dimava•14mo ago
Also maybe type.for sounds better if it's on type But again its double call
ssalbdivad
ssalbdivad•14mo ago
Unless you want type.declare<Foo>()({def: "string"})
Unknown User
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
ssalbdivad
ssalbdivad•14mo ago
declare<Foo>() creates a declaration for Foo
Unknown User
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Dimava
Dimava•14mo ago
If it's scope.for<S>().foo(fooDef) ... ... Okay I don't like it
ssalbdivad
ssalbdivad•14mo ago
The declaration has a .type function attached that lets you instantiate it Idk it seems reasonable and I trust my past self to have thought through the options carefully in a case like this .scope will be on the same declarereturn type
Dimava
Dimava•14mo ago
You need double generic (a provided type and a infered type)
ssalbdivad
ssalbdivad•14mo ago
Yeah the way it is is best. I think leveraging intuitions about "declaring" a type and later having to instantiate it is definitely clearer than for which is a reserved keyword and could also mean lots of other things Plus it's natural to leverage a TS keyword declare like this to parallel our use of type
Unknown User
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Dimava
Dimava•14mo ago
Xactly You either provide all our infer all
ssalbdivad
ssalbdivad•14mo ago
There is a proposal for this I said it should be <required, optional?>(required: required, optional: optional): unknown Basically if you pass any parameters, all the required parameters would have to be passed Just like now But optional parameters can not be passed and would be inferred
Dimava
Dimava•14mo ago
I'd say it should be f<T, infer S>
ssalbdivad
ssalbdivad•14mo ago
That makes it look like you can never pass it Which would be fine as a feature but it's misleading based on the proposal
Dimava
Dimava•14mo ago
Or f<T, S = infer>
ssalbdivad
ssalbdivad•14mo ago
Yeah that seems reasonable why didn't I like that as much I'm sure I explained it if you want to go find the issue I'm too tired
Unknown User
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
ssalbdivad
ssalbdivad•14mo ago
Yeah seems good makes me wonder why I didn't like it I know it was one of the issues suggested on the thread Or maybe I liked it relatively but I had some issue with it
Unknown User
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server