Kit
Functions

assertIsStringifiedNumber

assertIsStringifiedNumber(putativeNumber): asserts putativeNumber is StringifiedNumber

From time to time you might acquire a string, that you expect to parse as a Number, from an untrusted network API or user input. Use this function to assert that such an arbitrary string will in fact parse as a Number.

Parameters

ParameterType
putativeNumberstring

Returns

asserts putativeNumber is StringifiedNumber

Example

import { assertIsStringifiedNumber } from '@solana/rpc-types';
 
// Imagine having received a value that you presume represents some decimal number.
// At this point we know only that it conforms to the `string` type.
try {
    // If this type assertion function doesn't throw, then
    // Typescript will upcast `decimalNumberString` to `StringifiedNumber`.
    assertIsStringifiedNumber(decimalNumberString);
    // At this point, `decimalNumberString` is a `StringifiedNumber`.
    decimalNumberString satisfies StringifiedNumber;
} catch (e) {
    // `decimalNumberString` turned out not to parse as a number.
}

On this page