Matt Pocock Profile picture
Feb 13, 2023 13 tweets 4 min read Read on X
Let's talk about branded types.

If I were building an app where security was critical, I'd use the crap out of them. type User = {   id: string;...
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'. declare const brand: unique...
For instance, these two types are still strings, but they're now 'named' Password and Email. type Password = Brand<strin...
You can't pass a normal string to a slot that expects a password... const takesInPassword = (pa...
You'd need to 'cast' it to be a password... const takesInPassword = (pa...
Or, create a type predicate function that validates that it's a valid password. const isValidPassword = (  ...
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: Tooltip:  const password: &...
But outside it, it's just a regular string. Tooltip:  const password: &...
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? type User = {   id: string;...
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. type AuthorizedUser = Brand...
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.

totaltypescript.com/workshops/adva…
If you dug this thread, then give the original tweet an RT and let's spread TS awesomeness to more folks.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Matt Pocock

Matt Pocock Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @mattpocockuk

Apr 9
I've just been reading the Agent2Agent technical docs - Google's new protocol for agent to agent communication.

You know what that means. Let's diagram them: Image
Agents can be discovered by a public 'card' they expose via http. Image
There are three 'actors' in an A2A system. The user, the client, and the server. Not too tricky. Image
Read 5 tweets
Apr 9
Some notes on Cursor vs VSCode's new agent mode:
I'm currently working on a course where each lesson is a prompt, and you build a complex application from ~50 prompts.

This gives me a pretty gnarly eval for testing agent mode.

I've been using Cursor to build the course, but I tried VSCode instead.

How did it do?
Overall, VSCode performed well. The code produced by both was pretty decent, and they were comparable in terms of speed.

VSCode's agent was able to keep up with Cursor in terms of outputted code, with an important exception:
Read 8 tweets
Mar 25
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.
Read 7 tweets
Mar 7
MCP has ALREADY become an overloaded term.

Let's break it down into clients, servers and protocols: Image
MCP is built out of three ideas.

You have a client, a server, and a protocol to connect them together. Let's define each of them in turn.
The server is the thing that chats to the API's.

It can be on a remote server (i.e. in the cloud), or on your local system.

It contains all the code needed to actually take actions on the systems it talks to - sending Slack messages, creating files etc. Image
Read 7 tweets
Mar 6
Lots of folks feeling FOMO about MCP.

Here's the problem it solves: Image
Image
If you want to get your LLM to do useful things in the world, you will quickly hit a problem.

All the useful things in the world, like Slack, GitHub, or even your local filesystem, provide their own unique API to access them.
So, you end up writing a ton of code to glue your LLM and those API's together.

Want to add a new tool? That's more glue code. Grim.
Read 6 tweets
Oct 1, 2024
z.strict() fixed a nasty bug for me this morning.

You should probably be using it on most of the schemas that handle outside data.

Here's the bug 🧵 Image
I had a form where I was marking the date a post was posted. I was expecting a key of `postedAt`, with an optional datetime. Image
Except, in my form, I had gotten the `name` field wrong: Image
Read 7 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(