Fun fact: you can export a value and a type with the SAME NAME. This is great for when you have runtime things in your app that feel like types, like Zod schemas. Are you using this technique? I'm a convert.
I’ve tried that and the experience wasn’t great at all. Sometimes you need to import either the type, or the value (whatever that may be) and it gets really confusing.
I will use userSchema and User
Since you're exporting both, might be worth mentioning that if you want to import the value: import { User } from './user'; But if you want to import only the type: import type { User } from './user';
I hate this.
In my projects I always name the schemas "zUser" and the types "User". I think it makes it easy to instantly understand what are you referring to.
In my head this smells like conflict. Anyone can explain how this works and not breaks?
I would vote for more clarity. The one who wrote this file might know about the fact that 2 things have the same name in this file, but not necessarily the one importing them later. It's good to know it's possible, but I would avoid it 😅
Yes. However, the main pitfall of Zod is that it does not respect JSON schema. My favorite is ajv-ts package. Same API but respects JSON schema. https://www.npmjs.com/package/ajv-ts
Oh, this technique is brilliant. One is a type, one is a runtime value, so there is actually no conflict. Feel like would be an awesome boost to productivity easing naming headache 💡. I was always afraid this is going to cause some kind of name conflicts, but it seems like Typescript is smart enough to use the correct thing in the appropriate context
Solving web development problems with elegance and maintainable code
1yIs not junior friendly. "typeof User" and "User" will have different type. See you confused about which User I refer to.