alik0211 / mtproto-core

Telegram API JS (MTProto) client library for Node.js and browser
https://mtproto-core.js.org
GNU General Public License v3.0
625 stars 111 forks source link

Add types #285

Open xanderberkein opened 11 months ago

xanderberkein commented 11 months ago

This PR introduces types to the project and includes a generator script that auto-generates the types from the TL-schema included in this repository. Updating the types for a new API layer should be as easy as running the generator script.

The generated types in this PR (src/tl/types/schema.d.ts) have been included for clarity, but we can exclude them before merging, just like the generated builder and parser.

The type files consist of the following:

Using these types

All Telegram method names, parameters, and return types are fully typed. For example, let's have a look at the message.getHistory method from the documentation:

image



As the Telegram Type Language heavily relies on predicates (indicated by _ in TL) for return types, utilizing the types requires checking these predicates to ensure type safety:

image

Caveats

The Telegram Type Language is not strictly typed like Typescript. Method parameters may lack flags or an indication of being optional, but they could still be optional.

For instance, in the auth.sendCode method, phone_number is required, but api_id and api_hash are not. In the Telegram Type Language, there is no difference between these required and optional params. Therefore, in Typescript, they will all appear as optional.


Feel free to provide feedback. I have structured the types in a way that minimizes maintenance, making them easy to update when there are changes to the Telegram API layer. As long as the mtproto API remains unchanged, these types should require no further maintenance.

Closes https://github.com/alik0211/mtproto-core/issues/203

apoorvcodes commented 8 months ago

You've got a custom build for it?