You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Branded types in TypeScript refer to a technique to get faux-nominal types in a language that is otherwise structurally typed. By defining a type as follows:
import{z}from"zod"typeUserId=string&z.$brand<"userId">// zod is not actually needed, this is just an example
You will get a type error when passing a non-userId-id to a function that expects a userId-id.
Both Drizzle and Zod support branded types natively. However, in order to make effective use of branded types in a web app, they would also have to be supported at the route level. If not, an excessive amount of type casts are needed to use route params in e.g. database where clauses.
For example, currently in React Router, it is necessary to cast params.userId as follows:
This gets overly verbose rather quickly, and partially defeats the purpose of using branded types in the first place.
The most appropriate place to provide the type brands would probably be in the routes.ts file. For example, the CreateRouteOptions type could be extended with a brands property as follows:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Branded types in TypeScript refer to a technique to get faux-nominal types in a language that is otherwise structurally typed. By defining a type as follows:
You will get a type error when passing a non-
userId-id to a function that expects auserId-id.Both Drizzle and Zod support branded types natively. However, in order to make effective use of branded types in a web app, they would also have to be supported at the route level. If not, an excessive amount of type casts are needed to use route params in e.g. database
whereclauses.For example, currently in React Router, it is necessary to cast
params.userIdas follows:This gets overly verbose rather quickly, and partially defeats the purpose of using branded types in the first place.
The most appropriate place to provide the type brands would probably be in the
routes.tsfile. For example, theCreateRouteOptionstype could be extended with abrandsproperty as follows:Beta Was this translation helpful? Give feedback.
All reactions