You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
constres=broadcastTransaction(tx);console.log(res.txid);// okif(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
The text was updated successfully, but these errors were encountered:
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:Solution
Use discriminated unions for success vs error, and for each of the error types. Perhaps something like:
Additional context
The text was updated successfully, but these errors were encountered: