Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add types #285

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

xanderberkein
Copy link

@xanderberkein xanderberkein commented Aug 7, 2023

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:

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 #203

@apoorvcodes
Copy link

You've got a custom build for it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Where types for typescript in 6.0.0?
2 participants