iTranslated by AI
Creating a Function to Apply Branded Types to All Object Properties in TypeScript
Conclusion
export const withBrand = <T extends { [k: string]: unknown }, B extends string>(obj: T, brand: B) => {
return obj as Record<keyof T, T[keyof T] & { __Brand: typeof brand }>;
};

Explanation
Type Parameter <T extends { [k:string] unknown }, B extends string>
Here, two type parameters T and B are defined. T accepts an object type. B represents the type of the brand (unique identifier).
Function Parameters (obj: T, brand: B)
The function has two parameters. obj is the object to be branded, and brand is the brand itself.
Function Return Value Record<keyof T, T[keyof T] & { __Brand: B }>
This function returns a new object that retains the properties of the original object while adding a brand property __Brand to each property. This __Brand property has the type typeof brand of the original brand brand.
Type parameters T and B are used, but these are directly inferred from the function parameters obj and brand.
Situations where this function is useful
This function is useful when you want to notify the type system that a specific object plays a specific role. For example, you can make it so that a certain function only accepts objects of a specific brand as arguments.

That's it!
Discussion
これ便利ですね。ぼくもちょっとデモつくってみました。
定義側
使用側
簡単ですが、以上です。