Types
Telescope generates TypeScript types from Protocol Buffer definitions for easy integration with TypeScript projects. This page documents the key types generated by Telescope and how to use them.
Basic Types
Type Category | Description | Example |
---|---|---|
Message Types | TypeScript interfaces that match the structure of Protobuf messages | MsgSend , QueryBalanceRequest |
Enum Types | TypeScript enums that represent Protobuf enumerations | BondStatus , ResponseCheckTxType |
Service Types | Types for RPC service interfaces | MsgClientImpl , QueryClientImpl |
Registry Types | Types for registering and managing message types | GeneratedType , Registry |
Message Types
For each Protobuf message, Telescope generates several types and helper functions:
Generated Item | Description | Example |
---|---|---|
Interface | TypeScript interface matching the message structure | export interface MsgSend { ... } |
Constructor Functions | Functions to create message instances | export const MsgSend = { encode, decode, ... } |
Encoding Functions | Functions to serialize/deserialize messages | encode , decode , fromPartial , fromJSON |
Example Message Type
// Generated interface
export interface MsgSend {
fromAddress: string;
toAddress: string;
amount: Coin[];
}
// Generated static methods for the message
export const MsgSend = {
encode(message: MsgSend, writer: Writer = Writer.create()): Writer { ... },
decode(input: Reader | Uint8Array, length?: number): MsgSend { ... },
fromJSON(object: any): MsgSend { ... },
toJSON(message: MsgSend): unknown { ... },
fromPartial(object: DeepPartial<MsgSend>): MsgSend { ... }
}
Any Types
Telescope handles Protobuf's Any
type, which allows for dynamic typing:
export interface Any {
typeUrl: string;
value: Uint8Array;
}
Working with Any Types
import { Any } from "./google/protobuf/any";
import { MsgSend } from "./cosmos/bank/v1beta1/tx";
// Packing a message into Any
const msgSend: MsgSend = { ... };
const packedMsg = Any.pack(msgSend, "/cosmos.bank.v1beta1.MsgSend");
// Unpacking a message from Any (using interfaces)
const unpackedMsg = packedMsg.unpack(MsgSend);
Amino Types (for Legacy Compatibility)
For compatibility with legacy systems (like Keplr), Telescope generates Amino types:
Type | Description | Example |
---|---|---|
AminoMsg | Amino-compatible message format for signing | export interface AminoMsgSend { ... } |
AminoConverter | Converts between Amino and native formats | export const aminoConverter = { ... } |
Example Amino Types
export interface AminoMsgSend {
type: "cosmos-sdk/MsgSend";
value: {
from_address: string;
to_address: string;
amount: {
denom: string;
amount: string;
}[];
};
}
export const aminoConverters = {
"/cosmos.bank.v1beta1.MsgSend": {
aminoType: "cosmos-sdk/MsgSend",
toAmino: (message: MsgSend): AminoMsgSend["value"] => { ... },
fromAmino: (object: AminoMsgSend["value"]): MsgSend => { ... }
}
}
Registry Types
Telescope generates registry types for registering message types with various client libraries:
export const registry = [
["/cosmos.bank.v1beta1.MsgSend", MsgSend],
["/cosmos.staking.v1beta1.MsgDelegate", MsgDelegate],
// other message types...
];
export const load = (protoRegistry: Registry) => {
registry.forEach(([typeUrl, type]) => {
protoRegistry.register(typeUrl, type as GeneratedType);
});
};
Custom Type Handling
Telescope handles special types with custom encoding/decoding:
Type | TypeScript Representation | Notes |
---|---|---|
google.protobuf.Timestamp | Date or { seconds: Long; nanos: number } | Configurable via options |
google.protobuf.Duration | { seconds: Long; nanos: number } or string | Configurable via options |
cosmos.base.v1beta1.Coin | { denom: string; amount: string } | Represents blockchain tokens |
Int64 /Uint64 | bigint or Long | Configurable via options |
Client Types
Telescope generates several client types for interacting with blockchain nodes:
Client Type | Description | Example Usage |
---|---|---|
Query Client | For querying blockchain state | queryClient.getBalance(...) |
Tx Client | For sending transactions | txClient.send(...) |
RPC Client | For low-level RPC calls | rpcClient.abciQuery(...) |
LCD Client | For REST API calls | lcdClient.cosmos.bank.v1beta1.allBalances(...) |
Type Helper Functions
Function | Description | Example |
---|---|---|
DeepPartial<T> | Creates a type with all properties of T set to optional | DeepPartial<MsgSend> |
fromPartial<T> | Creates an object from a partial input | MsgSend.fromPartial({...}) |
toJSON | Converts a message to a JSON-compatible object | MsgSend.toJSON(msg) |
fromJSON | Creates a message from a JSON-compatible object | MsgSend.fromJSON({...}) |
Using Generated Types
import { MsgSend } from "./cosmos/bank/v1beta1/tx";
import { queryClient, txClient } from "./client";
// Create a message
const sendMsg = MsgSend.fromPartial({
fromAddress: "cosmos1...",
toAddress: "cosmos1...",
amount: [{ denom: "uatom", amount: "1000000" }]
});
// Query the blockchain
const balance = await queryClient.cosmos.bank.v1beta1.balance({
address: "cosmos1...",
denom: "uatom"
});
// Send a transaction
const result = await txClient.signAndBroadcast(
[sendMsg],
{ gas: "200000", amount: [{ denom: "uatom", amount: "5000" }] }
);
Advanced Type Features
Feature | Description |
---|---|
Union Types | Generated for oneOf fields in Protobuf |
Nested Types | Generated for nested message definitions |
Global Interface Registry | Manages type information for runtime reflection |
Type Information | Metadata attached to types for runtime operations |
Type Safety Considerations
- All generated types are strongly typed for TypeScript safety
- Optional fields are properly marked with
?
- Arrays and repeated fields are correctly typed
- Custom scalar types have proper handling and validation
- Functions include proper type checking