type Brand<T, B> = T & { readonly __brand: B };
type UserId = Brand<string, 'UserId'>;
type PostId = Brand<string, 'PostId'>;
const asUserId = (s: string): UserId => s as UserId;
const asPostId = (s: string): PostId => s as PostId;
function deleteUser(id: UserId) { /* ... */ }
const u = asUserId('abc');
const p = asPostId('xyz');
deleteUser(u); // ✓
deleteUser(p); // ✗ Type 'PostId' is not assignable to 'UserId'
deleteUser('raw'); // ✗ raw strings rejected
Create a free account and build your private vault. Share publicly whenever you want.