mtproto-core
mtproto-core copied to clipboard
Add types
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:
- the main
src/index.d.ts
file, which provides typings for the exposed classes and functions of this package - type definitions for the browser and node environments that simply re-export the types in index.d.ts. This is necessary to add typings for the different import paths.
- the generated TL schema typings in
src/tl/types/schema.d.ts
.
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:
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:
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
You've got a custom build for it?