npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@mtproto2/tl-schema

v0.1.0

Published

TL schema parser and TypeScript code generator

Readme

@mtproto2/tl-schema

TL (Type Language) schema parser and TypeScript code generator for MTProto 2.0.

Reads .tl schema files and generates strongly-typed TypeScript interfaces, constructor ID maps, and a serializer registry.

Installation

npm install @mtproto2/tl-schema

API

Parser

Parses raw TL schema text into an abstract syntax tree.

import { parseTLSchema } from '@mtproto2/tl-schema';
import type { TLSchema, TLConstructor, TLParam } from '@mtproto2/tl-schema';

const schema: TLSchema = parseTLSchema(tlSchemaText);

// schema.constructors -- array of TLConstructor (type constructors)
// schema.functions    -- array of TLConstructor (RPC methods)
// schema.layer        -- API layer number (e.g., 216)

The parser handles:

  • Constructors and methods (separated by ---functions---)
  • Conditional fields (flags.N?Type)
  • Vector types (Vector<Type>)
  • Namespaces (messages.sendMessage)
  • Bare types (lowercase) vs boxed types (uppercase)
  • CRC32 constructor IDs (#hexvalue)
  • Layer annotations

AST Types

interface TLParam {
  name: string;
  type: string;
  isFlag: boolean;
  flagField: string | null;   // e.g., "flags"
  flagIndex: number | null;   // e.g., 0, 1, 2
  isVector: boolean;
  innerType: string | null;   // inner type if vector
  isBareType: boolean;
  isTrueFlag: boolean;        // flags.N?true (presence flag, no data)
}

interface TLConstructor {
  name: string;               // e.g., "messages.sendMessage"
  id: number;                 // CRC32 constructor ID
  namespace: string | null;   // e.g., "messages"
  localName: string;          // e.g., "sendMessage"
  params: TLParam[];
  type: string;               // result type
  isFunction: boolean;
}

interface TLSchema {
  constructors: TLConstructor[];
  functions: TLConstructor[];
  layer: number;
}

Generator

Generates TypeScript source files from a parsed schema.

import {
  generateTypeScript,
  generateSerializerRegistry,
  generateAll,
  mergeSchemas,
  crc32,
} from '@mtproto2/tl-schema';
import type { GeneratedFiles } from '@mtproto2/tl-schema';

// Generate all output files from raw TL text
const files: GeneratedFiles = generateAll(apiTL, mtprotoTL);
// files['types.ts']          -- TypeScript interfaces
// files['constructorIds.ts'] -- Constructor ID map
// files['registry.ts']       -- Serializer registry
// files['index.ts']          -- Barrel export

// Or use individual generators for fine-grained control:
const schema = parseTLSchema(tlText);
const { types, constructorIds } = generateTypeScript(schema);
const registry = generateSerializerRegistry(schema);

// Merge two schemas (e.g., api.tl + mtproto.tl)
const merged = mergeSchemas(apiSchema, mtprotoSchema);

// Compute CRC32 of a string (used for constructor ID generation)
const id = crc32('user#d23c81a3');

Generated Output

The generator produces four files:

| File | Contents | |------|----------| | types.ts | TypeScript interfaces for every constructor and method | | constructorIds.ts | Map from constructor ID (number) to constructor name, and reverse | | registry.ts | Serializer registry with field descriptors for each constructor | | index.ts | Barrel exports |

Schema Update Workflow

To update the TL types to a newer Telegram API layer:

# 1. Fetch the latest schema files
npm run fetch-schema

# 2. Compare with the current schema
npm run diff-schema

# 3. Regenerate TypeScript types
npm run generate

# 4. Run tests to verify
npx vitest run

The generated files are written to packages/tl-types/src/generated/.

License

MIT