-
Notifications
You must be signed in to change notification settings - Fork 9
Approach to Branded Types & RPC-related Validation #96
Comments
Yes, branded types give us much better DX, not just because of safety, but because they describe what things are. However, I think we should rework our approach to branded types. type Branded<T, Brand extends PropertyKey, V = undefined> = T & Record<Brand, V>;
declare const _hex: unique symbol;
export type Hex<T extends Uint8Array = Uint8Array> = Branded<string, typeof _hex, T>;
declare const _encoded: unique symbol;
export type Encoded<T> = Branded<Uint8Array, typeof _encoded, T>;
declare const _hash: unique symbol;
export type Hash<T = unknown, K extends HasherKind = HasherKind> = Branded<Uint8Array, typeof _hash, [T, K]>
export type HexEncoded<T> = Hex<Encoded<T>;
export type HexHash<T = unknown, K extends HasherKind = HasherKind> = Hex<Hash<T, K>>; With this, instead of manually creating a branded export function encodeHex<T>($t: Codec<T>, value: T): HexEncoded<T> {
...
}
export function decodeHex<T>($t: Codec<T>, hex: HexEncoded<T>): T {
...
} And then type-safely call |
I like this approach of integrating branded types into our conversion utils. Nice. That signature–– Why the |
Also, I'm not sure if the branded types belong in |
Because, ultimately, the data contained in a hex string is a It also opens up the possibility of signatures like this in our export function encode<T extends Uint8Array>(array: T): Hex<T> {
...
}
export function decode<T extends Uint8Array>(hex: Hex<T>): T {
...
}
Perhaps they should be moved in with |
|
Do
U.HexString
and comparable branded types buy us much in terms of safety / DX?The text was updated successfully, but these errors were encountered: