Usage with TypeScript
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 installation guide.
Extract types of methods exposed by SDK
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 Parameters and ReturnType. For example,
import { walletDetector } from '@aeternity/aepp-sdk';
type WDCallback = Parameters<typeof walletDetector>[1];
type Wallet = Parameters<WDCallback>[0]['newWallet'];
let wallet: Wallet | null = null;
const stop = walletDetector(connection, ({ newWallet }) => {
wallet = newWallet;
stop();
});The same for ReturnType:
import { unpackDelegation } from '@aeternity/aepp-sdk';
type DlgUnpacked = ReturnType<typeof unpackDelegation>;
let delegation: DlgUnpacked | null = null;
delegation = unpackDelegation(
'ba_+EYDAaEBXXFtZp9YqbY4KdW8Nolf9Hjp0VZcNWnQOKjgCb8Br9mhBV1xbWafWKm2OCnVvDaJX/R46dFWXDVp0Dio4Am/Aa/Z2vgCEQ==',
);Initialize parameters with specific types
You may need to define an object with parameters to call an sdk method. Obvious to try it as
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 packEntry. To fix this you may define gaAuthData's type explicitly, like:
Or to define gaAuthData as immutable:
In the last case, txHash's type will be exactly "th_2CKnN6EorvNiwwqRjSzXLrPLiHmcwo4Ny22dwCrSYRoD6MVGK1", making it compatible with packEntry.
Some sdk methods return a union of multiple types. For example, unpackTx returns a union of all supported transaction fields. To work correctly you need to narrow this type to a specific transaction before accessing its fields. For example,
Without checking the tx.tag TypeScript will fail with
Property 'amount' does not exist on type 'TxUnpackedSignedTx1 & { tag: Tag; }'.
The above check is also implemented in unpackTx itself, instead of checking the tx.tag you can provide Tag in the second argument:
But if you need to get SpendTx properties inside a SignedTx you still need to use the above tag check.
You may find that unpackTx is a generic function so that it can be executed as
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).
Functions to assert types of user-provided data
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. spend method accepts the address as Encoded.AccountAddress, 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 spend 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 Encoded.AccountAddress because isEncoded implicitly marks address as ak_${string} in case it returns true.
Additionally, you can use isEncoded to validate data against other address types:
Or encoding types in general:
AENS name validation
The similar way isName can be used
If you don't need to handle invalid names specially then you can use ensureName:
Doing this way, ensureName will throw an exception if nameAsString is not a proper AENS name. TypeScript will handle nameAsString as ${string}.chain in lines below ensureName invocation.
Check types of contract methods
By default, it is allowed to call any method of the Contract instance. You can enable type-checking by providing a contract interface in a generic parameter of Contract. For example:
If you need to define the contract interface separately then extend ContractMethodsBase:
It is theoretically possible to generate a contract interface by ACI. But unfortunately, it is not supported currently.
Last updated
Was this helpful?