A
arktypeโ€ข4mo ago
PIat

Default number

Hello! Setting this brings the expected result, when ran through inferAmbient:
const queryType = type({
page: 'number',
})

// Result:
number
const queryType = type({
page: 'number',
})

// Result:
number
But adding a default value returns a function signature:
const queryType = type({
page: 'number = 1',
})

// Result
page?: ((In?: number | undefined) => Default<1>) | undefined
const queryType = type({
page: 'number = 1',
})

// Result
page?: ((In?: number | undefined) => Default<1>) | undefined
How can I get the number type in the editor when providing a default value?
86 Replies
TizzySaurus
TizzySaurusโ€ข4mo ago
It's not clear to me what exactly you're after here, but maybe t["infer"] or t["tOut"] is what you're after? Where t in this case is your queryType
ssalbdivad
ssalbdivadโ€ข4mo ago
Yeah you probably want t.infer for the output or t.inferIn for the input type (would allow undefined). tOut is more internal as it contains constraint brands
PIat
PIatOPโ€ข4mo ago
Thank you!!! inferIn is exactly what I needed I am using Arktype as my form, params and query schema, and am inferring the types from the schema for type-safe endpoints
ssalbdivad
ssalbdivadโ€ข4mo ago
If you want to create it from typed input, there's also a from method that accepts it Like queryType.from({}) Would result in {page: 1}
PIat
PIatOPโ€ข4mo ago
And if I do
queryType.from({
page: 20
})
queryType.from({
page: 20
})
the result will be {page: 20}?
ssalbdivad
ssalbdivadโ€ข4mo ago
Yeah it should
PIat
PIatOPโ€ข4mo ago
So for form parsing it's a better variant than doing
queryType({ page: 20 })
queryType({ page: 20 })
?
ssalbdivad
ssalbdivadโ€ข4mo ago
If you're ever passing in known data like that, I'd use from so you get type safety The default method is for accepting unknown
PIat
PIatOPโ€ข4mo ago
Actually it was infer, for the output as you said ๐Ÿ˜… But other than that, they both produce the same result on runtime?
ssalbdivad
ssalbdivadโ€ข4mo ago
Yes, under the hood it just calls assert which is also identical to the base method other than that if there are errors, it will throw instead of returning an errors object But it looks like in a case like that where you're passing an object literal there should never be errors
PIat
PIatOPโ€ข4mo ago
There will be errors if someone passes a wrong type
ssalbdivad
ssalbdivadโ€ข4mo ago
So in your case queryType({page: 20}) is just an exam,ple
PIat
PIatOPโ€ข4mo ago
If they send
queryType({ page: "some string" })
queryType({ page: "some string" })
ssalbdivad
ssalbdivadโ€ข4mo ago
But doesn't TS enforce that anyways? or would they not really be passing an object literal It seems like if they passed something invalid you would want to throw
PIat
PIatOPโ€ข4mo ago
They're always passing an object literal (Remix converts formData and query params into an object), but they might be passing wrong keys or types if they'll call it through some other client than the website
ssalbdivad
ssalbdivadโ€ข4mo ago
.from is just like .assert at runtime, just it accepts typed input The only difference between the base method and .assert is whether you want to get an ArkErrors return or throw directly, which generally depends on if you can handle an error or you just want to crash if the data is invalid (still crash with the same good error message)
PIat
PIatOPโ€ข4mo ago
In my case I return the errors and show them in a toast message, or if it's a form, then under the wrong fields (that's why I made the simple i18n POC ๐Ÿ˜ )
ssalbdivad
ssalbdivadโ€ข4mo ago
Use the root method then, but since you're not the one passing the data, .from would be irrelevant anyways
PIat
PIatOPโ€ข4mo ago
I understand this 100% now
ssalbdivad
ssalbdivadโ€ข4mo ago
Good, you can right some docs for me ๐Ÿ˜›
PIat
PIatOPโ€ข4mo ago
Thank you so much, I won't be guessing anymore I would be down if I were more helpful than a nuisance ๐Ÿ‘€
PIat
PIatOPโ€ข4mo ago
I really like the Fumadocs bar TOC ๐Ÿ˜†
No description
PIat
PIatOPโ€ข4mo ago
To be honest, I really love the Arktype website. You enforced modern design language with great illustrations, fonts and colors and the responsiveness is great
ssalbdivad
ssalbdivadโ€ข4mo ago
You saw the issue I just created I assume?
PIat
PIatOPโ€ข4mo ago
Yes
ssalbdivad
ssalbdivadโ€ข4mo ago
I would want it to look very similar to how it does now, I just don't like dealing with astro components
PIat
PIatOPโ€ข4mo ago
It's so good
PIat
PIatOPโ€ข4mo ago
Stuff like this too
No description
PIat
PIatOPโ€ข4mo ago
Makes the homepage feel alive It's very impressive you know ๐Ÿ‘€
ssalbdivad
ssalbdivadโ€ข4mo ago
Yeah haha I'm definitely proud of the code blocks I spent a while on those Now to create all the content that uses them ๐Ÿ˜› All the customizations were with CSS + shiki really so it should be translating to whatever is in next It's a lot of the same underlying tooling
PIat
PIatOPโ€ข4mo ago
I'll probably set it as my browser homepage once it's done ๐Ÿ˜
ssalbdivad
ssalbdivadโ€ข4mo ago
Just gotta change some underlying css variable names etc. Hopefully you don't need the docs so often that that becomes necessary ๐Ÿ˜›
PIat
PIatOPโ€ข4mo ago
I worked with Astro some 1.5 years ago because back then I really liked the idea of not being locked into a front-end framework, since I was experimenting with the frameworks to see what's most comfortable to me. When it came to dynamic and interactive content, it wasn't very great, since it's not the framework's focus and from what I remember there were some typing issues on the components
ssalbdivad
ssalbdivadโ€ข4mo ago
Yeah the TS editor experience kinda sucks They publish raw .ts files to their node_modules which always gives me errors in my IDE And it's been really hard to reliably get types/errors without a bunch of nonsense in the astro components + mdx It kinda seems like on the one hand it's not locking you in, but on the other it's locking you in to astro ๐Ÿ˜›
PIat
PIatOPโ€ข4mo ago
Yeah, you have to set it up very specifically
ssalbdivad
ssalbdivadโ€ข4mo ago
Right and I've already spent more time on that stuff than I want I just want React
PIat
PIatOPโ€ข4mo ago
That was my conclusion too
ssalbdivad
ssalbdivadโ€ข4mo ago
And I guess to be able to reference that stuff directly in MDX, but I don't need anything else You wouldn't believe the hacks I ended up using to get all the highlighting to work between astro/starlight and shiki lol
PIat
PIatOPโ€ข4mo ago
Wait, all this to make the copy button? Because you can't add React components directly?
ssalbdivad
ssalbdivadโ€ข4mo ago
Yeah hooking into whatever they were doing was ridiculously complicated
PIat
PIatOPโ€ข4mo ago
Crazy...
ssalbdivad
ssalbdivadโ€ข4mo ago
Basically if I had just been able to use the default config options it would have been great
PIat
PIatOPโ€ข4mo ago
Well you had to use default web js
ssalbdivad
ssalbdivadโ€ข4mo ago
But because what I needed wasn't supported there, customizing it was awful
PIat
PIatOPโ€ข4mo ago
Yeah... Surprising Strange thing, Shiki
PIat
PIatOPโ€ข4mo ago
You had to add button html as a string literal ;-;
No description
PIat
PIatOPโ€ข4mo ago
Then add those hooks ;-;
ssalbdivad
ssalbdivadโ€ข4mo ago
WTF is this
No description
PIat
PIatOPโ€ข4mo ago
A bear! Duck!
ssalbdivad
ssalbdivadโ€ข4mo ago
Well yeah those are chill But I just don't understand how I'm supposed to write this syntax so the parser doesn't blow up in mdx
PIat
PIatOPโ€ข4mo ago
Not sure where you have an ! there
ssalbdivad
ssalbdivadโ€ข4mo ago
There is no !, it's some nonsense way later in the file that shouldn't affect anything the parser is just bad
PIat
PIatOPโ€ข4mo ago
Maybe they expect "type(\"string\")" instead of 'type("string")'
ssalbdivad
ssalbdivadโ€ข4mo ago
I tried that too Oh I figured it out it's because I renamed the file to mdx figuring it was a superset of md but <!-- comments I guess aren't supported? So it was related to ! at least
PIat
PIatOPโ€ข4mo ago
Huh
ssalbdivad
ssalbdivadโ€ข4mo ago
But the syntax highlighting is really unreliable and I've seen tons of cases where the editor extension does crazy stuff when I'm typing random content like turning my words into imports from TS and adding them to the bottom of the file in broken syntax https://discord.com/channels/830184174198718474/1274367848474742845
PIat
PIatOPโ€ข4mo ago
Yeah, it's just awful You're just writing in notepad at that point This is really surprising I'll have to install remark-mdx-html-comment then
ssalbdivad
ssalbdivadโ€ข4mo ago
Yeah I don't see any reason it wouldn't be a superset
PIat
PIatOPโ€ข4mo ago
I write my notes in Markdown. And all the posts on our website are done in Markdown I went with Markdown directives instead of MDX for this exact reason Markdown is eternal, and my directive parser will work in 20 years, MDX seems to be everupdating But of course I don't have the IDE needs, it's done with a simple markdown editor that parses the directives I really found it simpler than to import components in the files ๐Ÿ˜
ssalbdivad
ssalbdivadโ€ข4mo ago
Or like this that is happening to me now, so confusing
PIat
PIatOPโ€ข4mo ago
Another part was non-programming coworkers wouldn't understand the syntax that well and would mess up more than with directives, if they ever need to touch the markdown source Huh???? That's so annoying Do you need the { there?
ssalbdivad
ssalbdivadโ€ข4mo ago
Yeah I guess it's how they recommend doing comments in mdx
PIat
PIatOPโ€ข4mo ago
you can use JS(X) comments instead: {/* stuff */}. https://github.com/mdx-js/mdx/issues/1042#issuecomment-1008653291
GitHub
RFC: MDX comment syntax ยท Issue #1042 ยท mdx-js/mdx
As part of #1039 comment syntax currently only supports JS comment expressions: # Hello, world! {/* I am a comment */} This syntax is nice because it feels more JSX-y than HTML comments. However, i...
PIat
PIatOPโ€ข4mo ago
I can't find any recommendations in general, just the MS website
ssalbdivad
ssalbdivadโ€ข4mo ago
Honestly I'm just using Ctrl+/ to autocomment things and going with whatever it does
PIat
PIatOPโ€ข4mo ago
And it does that :/
ssalbdivad
ssalbdivadโ€ข4mo ago
So you can use root-level comments now?
PIat
PIatOPโ€ข4mo ago
GitHub
Multi-line comments in MDX are malformed ยท Issue #15163 ยท prettier/...
Prettier 3.0.0 Playground link --parser mdx --trailing-comma es5 Input: (whatever indentation or variation) # Hello, World! {/* This is a * multi-line comment */} Output: # Hello, World! {/* This ...
ssalbdivad
ssalbdivadโ€ข4mo ago
Nah it doesn't work Or at least with nested code blocks and stfuf it can't parse it And all the syntax highlighting breaks Oh well
ssalbdivad
ssalbdivadโ€ข4mo ago
@PIat more joy with mdx formatting
ssalbdivad
ssalbdivadโ€ข4mo ago
I thought I was going insane and was somehow adding the extra backticks myself
PIat
PIatOPโ€ข4mo ago
That's so bad But it seems like a Prettier issue?
ssalbdivad
ssalbdivadโ€ข4mo ago
I don't know who owns the mdx formatting. I'm using prettier-plugin-astro which theoretically should own making their core syntax work
PIat
PIatOPโ€ข4mo ago
Well code highlighting seems to be fine there at least ๐Ÿ‘€
ssalbdivad
ssalbdivadโ€ข4mo ago
Yeah I actually figured out a crazy hack to get those code blocks to work well within components. I don't think it was meant to work but it does and it's wayyyy better than what I thought I would have to do haha https://discord.com/channels/830184174198718474/1279159531280138250/1279203600714825869
PIat
PIatOPโ€ข4mo ago
Huhhh That's a wild syntax But the editor experience should be muuuch better now
ssalbdivad
ssalbdivadโ€ข4mo ago
I don't think it's meant to be supported but basically I can force it to render and I guess it respects the MDX syntax, which means I get highlighting ๐ŸŽ‰ I still have to be super careful with formatting because it arbitrarily fucks my shit up, but yeah when it works it's way better ๐Ÿ˜›
PIat
PIatOPโ€ข4mo ago
So you're staying with Astro?
ssalbdivad
ssalbdivadโ€ข4mo ago
I mean I'd still rather migrate so I have more control but I'm just happier to write a bunch of codeblocks in the meantime
PIat
PIatOPโ€ข4mo ago
Great ๐Ÿฅณ๐Ÿฅณ๐Ÿฅณ๐Ÿฅณ
ssalbdivad
ssalbdivadโ€ข4mo ago
I'm not going to spend the time doing that when there are lots of things only I can do. maybe someone else will want to do this and they could probably do it better ๐Ÿ™
PIat
PIatOPโ€ข4mo ago
You mean to migrate to new docs? ๐Ÿ‘€
ssalbdivad
ssalbdivadโ€ข4mo ago
Yeah I created that issue but I won't prioritize it unless someone else takes it up realistically
Want results from more Discord servers?
Add your server