const accountId: Brand<string, "Account"> = "123654"
Has the error Type 'string' is not assignable to type '{ readonly __Account_brand: never; }'You have to make a function to apply the brand via a cast, the article explains this as well.
function makeObjectId(id: string): ObjectId {
return id as ObjectId;
} const accountId = "125314" as AccountId
It makes sense that the technique uses casting.But what if I instead wrote:
const accountId = AccountId ("125314" );
There would be the needed checks and balances in the function
AccountId(). Wouldn't that do pretty much the same thing? const accountId = "125314" as AccountId;
or const accountId = new AccountId("125314");
or const accountId = accountIdFromString("125314");
is all "explicit" casting in my understanding, one way or the other, and the first ... as ... being on the lowest level.I'd rather go for something like "do the casting close to the source, and in general use 'parse, don’t validate'[0] when getting input from the outer world".
[0]: https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-va...
If you do add valuation, make it an honest asValidatedAccountId("123"), not an accountIdFromString("123") that may or may not do more than casting.
(PS, very much off topic: speaking of hidey-holes, are any of the AOP monstrosities still operational?)
const accountId = AccountId (“43525”)
being less explicit const myCompanyId = something_complicated as CompanyId;
where something_complicated actually is a UserId, not a bare string.It is way too easy to accidentally destroy type safety with `as`, there are absolutely 0 safeguards.
I fear every single instance of `as` in any Typescript source I see.
Another way you can implement the same: through a class constructor like new UserId('some string') which can also throw or let you handle operations on the class itself while allowing you to get the value.
I don't love that example though, because the brand field's value of a string literal will make it seem like the object actually has a property named `__brand` when it doesn't. `never` is the best brand field type as far as I can tell.
If you assert that your id's actually do have a never-typed field, and you let your program access it, you're basically letting users enter an impossible state freely and easily.
Ideally you could brand with a private field, but we would probably need `typeof class` for that (assuming `typeof class` allows private members. I'm not sure).
But from the direction of usage...because you've used casting to (as far as TypeScript is concerned) construct the value, once it's floating around you're in an impossible state - and no, having a branded thing should not be an impossible state. Because of that you can freely violate the principles of the type system's logic - ex falso quodlibet.
A never value is effectively an any value, and now you have one on hand at all times.
https://www.typescriptlang.org/play?#code/FAMwrgdgxgLglgewgA...
If by some mistake your code gets a reference to a field with a `never` type, you’ll never get a type error passing or assigning that reference downstream. That’s relatively trivial to address if you can spot the (edit: runtime) errors it causes directly, but can wreak terrible havoc if it finds a path through several layers of function calls or whatever other indirection before the runtime consequences surface.
No it isn't. If your ids do have a field, then marking them as never having a field is unwise. Never means that code that reads that field is never wrong (because you can never reach the point of reading it), whereas you want the opposite, code that reads that field is always wrong.
GP just focused on the never-part of your question.
I wouldn’t call myself an expert in TypeScript’s type system (although I use it daily), so I’m not sure about that one.
I do know I would have omitted readonly if I did this myself, but perhaps by mistake.
Is your problem the line wraps in the parent’s comment?
export type Brand<T, Brand extends string> = T & {
readonly [B in Brand as `__${B}_brand`]: never;
};
I count 14 different pieces of punctuation. you might as well use Perl at that point. export type Brand<T, Brand extends string> = T & {
readonly [B in Brand as `__${B}_brand`]: never;
};
It exports a freshly defined type named Brand, which is a generic type with two parameters, one named T (that can be any type), and other named Brand (probably should be named differently to avoid confusion with the name of the whole exported type). Brand parameter must be a type that's assignable to string. For example it can be a type that has only one specific string like "Account" allowed as a value. It might be a bit weird but in places where type is expected "Account" is not treated as string literal but as a type assignable to string with only one allowed value, "Account".This whole exported type is defined as an intersection type of parameter T and object type that has readonly properties for every type that is assignable to the type passed in as parameter named Brand when this generic type is instantiated. For example if "Account" was passed then B can only be "Account" here, so this object type will only contain a single property. Key of this property will be a string that looks like __Account_brand (if the Brand was "Account") and the type of this property will be never, so nothing is allowed to be assigned to this property.
The result of instantiation of this whole exported generic type will be a type which values can be assigned to variables of type T, but attempt to assign value of type T (or type branded with some other string) to variable of this branded type will result in an error because of missing or mismatched brand property.
This definition might allow for interesting complex branding like:
let id:Brand<string, "Employee" | "Manager">;
that can be assigned to variables and parameters of types Brand<string, "Employee"> or Brand<string, "Manager"> or just string.PS.
Intersection type constructed from multiple types in TS using & symbol is a type which values are assignable to variables of each of the constituent types.
For example value of a type Logger & Printer can be assigned to variables and parameters of type Logger and of type Printer as well.
If for example the language was missing builtin hashmap type, its implementation would be nasty as well.
I'm not a huge fan of typing systems, the thing I like the most in TypeScript is that you can use types as little as you want and add more only when you want it. But I don't like languages like go that intentionally lack features.
people will go as far down the rabbit hole as you let them, which is what the Go developers understand and are trying to account for:
from typing import NewType
You really don't need the symbol - you can use an obscure name for the branding field. I think it helps the type self-document in errors and hover-overs if you use a descriptive name.
I use branding enough that I have a little helper for it:
Use it like: And the hover-over type is: