Loading topic
OrderId. You pass it a UserId. The compiler says nothing, and the program loads the wrong record or fails at runtime. This is not a bug in the compiler. It follows from how TypeScript decides whether one type fits another. This chapter explains that rule, shows where it cause6 min readstring & { readonly __brand: "UserId" } makes a UserId that cannot be passed where an OrderId is required. He6 min readBrand<T, B> helper from the previous chapter. A brand only protects you if values get the brand honestly, and smart constructors are how that ha5 min readBrand<T, B>. It also assumes you can read generic functions and union narrowing, and that you know types are erased at runtime. Here you learn the two TypeScript features that let a runtime check produce a branded type without a cast 5 min readUserId and Email. Numbers have the same problem, and it is often more costly. A function that takes "a number" can't tell meters from seconds, or a valid port from -3. This chapter applies the brand pattern to numbers in two ways: to units (what the5 min readBrand<T, B> helper from the earlier chapters. You should also be comfortable with the difference between compile-time types and runtime values. Branded types only pay off once you decide where branded values are allowed t5 min readBrand<T, B> helper from the earlier chapters and understand intersection types and unique symbol. It covers a lighter variant of branding called flavoring. You will learn how it works, what it allows and what it blocks, and how to decide between it and a5 min readBrand<T, B> helper, use a unique symbol as the tag key, and write a smart constructor. It also assumes you know how intersection types combine properties. Here you will make brands stackable, so that one brand refines another, and you will see where s5 min readBrand<T, B> helper, write smart constructors, and use type predicates. It covers where brands stop working. A brand is a compile-time label. It makes a promise only as long as every value that carries the label got it honestly.6 min readBrand<T, B> helper from earlier in this tutorial, and that you can read basic zod schemas. It covers the zod library's own brand method. You will see what it produces, how it fits boundary validation, and where it differs from a hand-rolled brand.5 min readprivate members, static methods).5 min readBrand<T, B> helper, a validating constructor, and a type5 min read