Typeguards

Description

When you retrieve data with GetMessage and GetMessages, you will not always know the type of the fetched data. For this reason Any provide Typeguards function that will allow auto-completion in typescript.

If you only want to retrieve the message type without the use of typeguards you can query the mesage.type attribute contain inside the BaseMessage object you retrieve.

Usage

Import the Any declaration to interact with the different kind of typeguards avalaible.

import { messages } from 'aleph-sdk-ts';

// Or used optimized import
import { any } from 'aleph-sdk-ts/dist/messages';

With Post

import { any } from 'aleph-sdk-ts/dist/messages';

(async () => {
    const res = await GetMessages({});
    if (any.is.Post(res))
        res.content.type
})();

With Aggregate

import { any } from 'aleph-sdk-ts/dist/messages';

(async () => {
    const res = await GetMessages({});
    if (any.is.Aggregate(res))
        res.content.key
})();

With Store

import { any } from 'aleph-sdk-ts/dist/messages';

(async () => {
    const res = await GetMessages({});
    if (any.is.Store(res))
        res.content.item_hash
})();

With Forget

import { any } from 'aleph-sdk-ts/dist/messages';

(async () => {
    const res = await GetMessages({});
    if (any.is.Forget(res))
        res.content.hashes
})();

With Program

import { any } from 'aleph-sdk-ts/dist/messages';

(async () => {
    const res = await GetMessages({});
    if (any.is.Program(res))
        res.content.code
})();

Last updated