Matt Pocock’s Post

View profile for Matt Pocock, graphic

Author of Total TypeScript

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.

  • No alternative text description for this image
Akhmad Salafudin

Solving web development problems with elegance and maintainable code

1y

Is not junior friendly. "typeof User" and "User" will have different type. See you confused about which User I refer to.

Like
Reply
⚡️ Alejandro Nanez

Senior Software Engineer @ GlossGenius | Typescript | React | Next.js | Technical Lead | Software Engineering instructor @ Egghead.io

1y

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.

Carlos Ziegler

Full Stack Developer at MediaMarktSaturn

1y

I will use userSchema and User

Stevan Kapicic

Senior Software Engineer

1y

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';

Ron Mizrahi

Frontend Specialist | Platform Lead @ Stealth

1y

I hate this.

André Batista

𝗦𝗲𝗻𝗶𝗼𝗿 𝗙𝗿𝗼𝗻𝘁𝗲𝗻𝗱 𝗘𝗻𝗴𝗶𝗻𝗲𝗲𝗿 @ Coalition, Inc

1y

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.

Nicodemos Santos

Full-stack Laravel | React | React Native Developer at Advanced Sponsorship Insights

1y

In my head this smells like conflict. Anyone can explain how this works and not breaks?

Ahmed Hassanein

🌐 Frontend Lead / 🧑🏫 Mentor / 👀 Available 🇵🇸🇱🇧🇸🇩

1y

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 😅

Like
Reply
Vitali Haradkou

Senior SDET / certified node.js application developer JSNAD

1y

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

Like
Reply
Bao Huynh

Backend Engineer @ OLR // React, Typescript, Nodejs, Python, Elixir // I build functional, practical, and beautiful applications, from frontend to backend

1y

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

Like
Reply
See more comments

To view or add a comment, sign in

Explore topics