Usage with TypeScript
Last updated
Was this helpful?
Last updated
Was this helpful?
This guide explains handling the edge cases that can occur while using aeternity SDK in a TypeScript project.
Firstly, ensure you've set up TypeScript according to the .
SDK doesn't expose types separately to reduce the number of exports and simplify tracking of breaking changes. But you may need these types to prepare parameters or to hold the return value. In such cases, it is advised to use TypeScript-provided generics and . For example,
The same for :
You may need to define an object with parameters to call an sdk method. Obvious to try it as
Or to define gaAuthData
as immutable:
Without checking the tx.tag
TypeScript will fail with
Property 'amount' does not exist on type 'TxUnpackedSignedTx1 & { tag: Tag; }'.
But if you need to get SpendTx properties inside a SignedTx you still need to use the above tag
check.
The problem is that JavaScript won't check if the transaction is a SpendTx, so provide Tag.SpendTx
as the second argument instead (as the above).
Or encoding types in general:
The problem in this case, is that TypeScript will generalize the type of unpackedEntry.txHash
to string
instead of th_${string}
making it incompatible with arguments of . To fix this you may define gaAuthData
's type explicitly, like:
In the last case, txHash
's type will be exactly "th_2CKnN6EorvNiwwqRjSzXLrPLiHmcwo4Ny22dwCrSYRoD6MVGK1"
, making it compatible with .
Some sdk methods return a of multiple types. For example, returns a union of fields. To work correctly you need to narrow this type to a specific transaction before accessing its fields. For example,
The above check is also implemented in itself, instead of checking the tx.tag
you can provide Tag in the second argument:
You may find that is a generic function so that it can be executed as
Let's assume we need to receive an address from the user to send some coins to it. The user enters an address in a text box, we can get it as a string. method accepts the address as , it won't accept a general string. We can overcome this restriction by adding a type assertion, like:
The problem is that TypeScript won't check if address
is an ak_
-encoded string, and the method will fail in this case.
A more accurate solution would be to check the address
in advance, providing user feedback if it is incorrect. For example:
Please note that this method doesn't require explicit casting string
to because implicitly marks address
as ak_${string}
in case it returns true
.
Additionally, you can use to validate data against other address types:
The similar way can be used
If you don't need to handle invalid names specially then you can use :
Doing this way, will throw an exception if nameAsString
is not a proper AENS name. TypeScript will handle nameAsString
as ${string}.chain
in lines below invocation.
By default, it is allowed to call any method of the instance. You can enable type-checking by providing a contract interface in a generic parameter of . For example:
If you need to define the contract interface separately then extend :
It is theoretically possible to generate a contract interface by ACI. But unfortunately, it is currently.