This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
src/index.d.ts
file, which provides typings for the exposed classes and functions of this packagesrc/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, butapi_id
andapi_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