If I were building an app where security was critical, I'd use the crap out of them.
A Brand type lets you use a bit of 'nominal' typing inside TypeScript.
By declaring a unique symbol, 'brand', we can use this Brand type helper to create types with 'names'.
For instance, these two types are still strings, but they're now 'named' Password and Email.
You can't pass a normal string to a slot that expects a password...
You'd need to 'cast' it to be a password...
Or, create a type predicate function that validates that it's a valid password.
This 'type predicate' approach is super nice, because you can take in the un-branded input and get out the branded output.
You can see that, inside the if statement where we check it's valid, the password IS valid:
But outside it, it's just a regular string.
Branded types are extremely powerful for security-critical systems, like finance apps, which require a lot of validations and checks before actions can be performed.
For instance, what's wrong with this picture?
That's right - makePayment can be called using _any_ user, whether they've been validated or not.
Using a branded user, you can ensure that makePayment only ever gets called with a user that has already been validated.
Of course, you'll have unit tests covering this behaviour.
But layering your types on top will give you a whole extra level of security and peace of mind, as well as documenting exactly where validation checks are performed.
I thought branded types were so useful that I made a whole section about them in Total TypeScript, my comprehensive TS course.
It's in the 'Advanced TypeScript Patterns' workshop, along with classes, type predicates, globals and working with libraries.
Ohhhhhh I think I've found an AI writing flow I'm happy with
Need to iterate on it before I write about it more, but it's really working.
Essentials are:
1. Create a big multi-step plan for the article, including checkboxes. On the current article I'm working on, it's 16+ steps. 2. For each step, ask it for 3 different plans for how to write the section. Give the section a target word count.
3. Get it to apply this to the document. 4. Do passes for paragraph length and section headings before even reading the paragraphs. 5. Do passes for writing style, analogies. 6. Get it to rewrite things in lists if needed.