Open
Description
Problem
The way the types are currently constructed for TxBroadcastResult
,
stacks.js/packages/transactions/src/types.ts
Line 156 in 50399dc
makes it "impossible" to check for success or error with TS. The union only has the txid
prop common among all possible types, and therefore it's the only prop TS allows checking:
const res = broadcastTransaction(tx);
console.log(res.txid); // ok
if (res.error) {} // TS Error, prop `error` doesn't exist on all instances of TxBroadcastResult
Solution
Use discriminated unions for success vs error, and for each of the error types. Perhaps something like:
type TxBroadcastResult = // Key `"type"` is the discriminator
| { type: "success"; txid: string }
| { type: "error"; detail: ErrorDetail };
type ErrorDetail = // Key `"reason"` is the discriminator
| {
reason: "Serialization";
txid: string;
// other useful fields
}
| {
reason: "SignatureValidation";
txid: string;
// other useful fields
};
// ... other error types
Additional context
Metadata
Metadata
Assignees
Type
Projects
Status
📋 Backlog