Recently, I've been exploring a JavaScript library known as Automerge. Within this library, there is a type declaration that looks like the following:
type BinaryDocument = Uint8Array & { __binaryDocument: true }
It's important to note that the property __binaryDocument
has a literal value of true.
I've been wondering, what would be the best way to instantiate such a type in a clean and concise manner?
The approach I came up with may seem a bit excessive:
class BinaryDocument extends Uint8Array {
__binaryDocument: true
constructor(val: Uint8Array) {
super(val)
}
static from(buffer: Uint8Array): BinaryDocument {
return new BinaryDocument(buffer)
}
}