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:
typeTxBroadcastResult=// Key `"type"` is the discriminator|{type: "success";txid: string}|{type: "error";detail: ErrorDetail};typeErrorDetail=// 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:
Ran into the same issue. Not sure why this wasn't a problem before. The idiomatic solution is to check for error using `'error' in res', but we can think about adding a type discriminator as well (that would however add additional synthetic information that the node isn't responding with)
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: