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

Migrate partially to TS #1108

Merged
merged 90 commits into from
Aug 22, 2020
Merged

Migrate partially to TS #1108

merged 90 commits into from
Aug 22, 2020

Conversation

KnorpelSenf
Copy link
Collaborator

@KnorpelSenf KnorpelSenf commented Aug 18, 2020

Description

Tighten TS config, eliminate most JS files, add missing types for most files. Also, the project did not follow its own prettier config, I reformatted the code in these places and removed prettier-ignore comment where I touched the code anyway.

The only thing currently missing is updating the types provided by telegram-types so that they are reflecting the API in version 4.9.

src/markup.ts would have a few errors. I fixed #1076 (comment), but what about #1076 (comment)? This calls for breaking changes. Some other dependant methods would have errors as a side-effect, they need to be fixed, too. This PR currently only suppresses all of the compilation errors by casting to any where necessary, but markup.ts should be rewritten in the future.

Type of change

This PR mainly changes typings.

How Has This Been Tested?

  • npm run typecheck

Otherwise, thorough testing is still an open task for this PR. It needs to be verified that no breaking changes other than the intended ones are introduced. Especially testing the lib by using it in other projects is important, so most likely will some types need to be changed again later on.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works (does not apply)
  • New and existing unit tests pass locally with my changes

Copy link
Member

@wojpawlik wojpawlik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting-only changes are not welcome.

Do you have any reason against using import type?

src/composer.ts Outdated Show resolved Hide resolved
src/context.ts Outdated Show resolved Hide resolved
src/session.ts Outdated Show resolved Hide resolved
src/core/network/client.ts Outdated Show resolved Hide resolved
src/core/network/client.ts Outdated Show resolved Hide resolved
src/extra.ts Outdated Show resolved Hide resolved
src/extra.ts Outdated Show resolved Hide resolved
src/telegraf.ts Outdated Show resolved Hide resolved
tsconfig.json Outdated Show resolved Hide resolved
src/session.ts Outdated Show resolved Hide resolved
@KnorpelSenf
Copy link
Collaborator Author

I know that formatting-only changes are not welcome. We avoid them using Prettier. However, if someone commits code to the repo that does not follow the formatting conventions, I believe it is better to fix the formatting once and do it properly rather than permitting that people keep on submitting formattings that follow their own rules. That way, we can keep everyone in sync.

I don't personally have something against import type declarations, I understand their purpose. I suggest following the recommendations from the TS team. I quote:

This feature is something most users may never have to think about; however, if you’ve hit issues under --isolatedModules, TypeScript’s transpileModule API, or Babel, this feature might be relevant.

What they're basically saying is that this should only be used when necessary. telegraf applied them inconsistently and without having a reason to do so. I'm completely fine with using them everywhere if that is desired for some reason, however, as they were used only sparingly, I decided that the consistent state of not using them at all is better than the consistent state of using them wherever possible.

@KnorpelSenf
Copy link
Collaborator Author

KnorpelSenf commented Aug 19, 2020

Is there anything you can say about the three major points I listed in the description? I'd be happy to see the remaining 37 compilation errors fixed

Copy link
Member

@wojpawlik wojpawlik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for being terse, there's a lot to cover!

By "formatting-only changes" meant the extra newlines in payload objects in telegraf.ts, please revert those.

1. src/core/replicators.ts has a lot of errors because I don't really know what type message has in all these methods. Who would invoke them anyway?

Replicators are used by Telegram::sendCopy. You can try using Required<Pick<Message, ...>> as param types.

3. src/scenes/context.ts has a few lines where this.current is accessed—I really don't know how that is supposed to behave.

What's the problem?

get current () {
const sceneId = this.session.current || this.options.default
return (sceneId && this.scenes.has(sceneId)) ? this.scenes.get(sceneId) : null
}

4. Also confer #1074 (comment) for a few more things that are planned. They could be done here, as well.

Please don't expand the scope of this PR, it's huge already! I'm considering requesting splitting it up.

src/core/network/client.ts Outdated Show resolved Hide resolved
src/util.ts Outdated Show resolved Hide resolved
Co-authored-by: Wojciech Pawlik <woj.pawlik@gmail.com>
Co-authored-by: Wojciech Pawlik <woj.pawlik@gmail.com>
Copy link
Member

@wojpawlik wojpawlik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just, updating these types will be quite a lot of work I believe, and I'd take the opportunity to rethink structure of replicators. I don't like that copyMethods is on the same level as text for example... I'd rather export { copyMethods, replicators }.

Similarly, I'd rather not touch markup and perhaps session at all than doing a poor job that needs immediate fixing.

For reference: https://github.com/telegraf/telegraf/blob/60635c3/src/markup.ts, https://github.com/telegraf/telegraf/blob/60635c3/src/core/replicators.ts, https://github.com/telegraf/telegraf/blob/60635c3/src/session.ts.

src/composer.ts Outdated Show resolved Hide resolved
src/composer.ts Outdated Show resolved Hide resolved
src/composer.ts Outdated Show resolved Hide resolved
src/extra.ts Outdated Show resolved Hide resolved
src/extra.ts Outdated Show resolved Hide resolved
KnorpelSenf and others added 9 commits August 21, 2020 14:27
Co-authored-by: Wojciech Pawlik <woj.pawlik@gmail.com>
Co-authored-by: Wojciech Pawlik <woj.pawlik@gmail.com>
Co-authored-by: Wojciech Pawlik <woj.pawlik@gmail.com>
Co-authored-by: Wojciech Pawlik <woj.pawlik@gmail.com>
Co-authored-by: Wojciech Pawlik <woj.pawlik@gmail.com>
@KnorpelSenf
Copy link
Collaborator Author

KnorpelSenf commented Aug 21, 2020

Well the problem is that these types are also directly exposed to library consumers. telegraf is not fully usable with TS without the types because how would someone work with polls without telegraf providing poll on the Message type? We could only allow arbitrary keys and then live with all the problems that come with it.

I agree on all the other stuff you said but I still don't quite see how we could switch to generated .d.ts files without a full coverage of the Telegram API … IMO do we need to fix that script generating types.

For this PR we most likely have to re-enable allowJs again. This really is a pity because it makes the whole work superfluous for my use-case.
EDIT: I saw you just did

@KnorpelSenf KnorpelSenf changed the title Migrate to TS Migrate partially to TS Aug 21, 2020
@KnorpelSenf KnorpelSenf marked this pull request as ready for review August 21, 2020 13:41
Copy link
Member

@wojpawlik wojpawlik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One does not simply fix that script.

What use-case requires the library to be fully typed?


I'm nitpicking at this point. Great work, I'll merge it after 24 hours, in case me, you or @dotcypress spots something wrong. Looking forward to reviewing your subsequent PRs :)

src/composer.ts Outdated Show resolved Hide resolved
@KnorpelSenf
Copy link
Collaborator Author

One does not simply fix that script.

I see … I had a look at the repo and didn't feel like doing that either.

What use-case requires the library to be fully typed?

You might have heard of awilix and of its integration with express or koa. I sort of want to recreate awilix-koa for telegraf. In an ideal (~typed) world, I do not have to re-specify the entire list of available Telegram methods. Instead, I would like to stay so generic that updates to telegraf “automatically” become available in awilix-telegraf. I therefore want to do some type transformations on the API surface of telegraf and thus get my library from it. But this is really hard if not all methods are properly typed, including parameters and return types and everything. As my plan includes a quite deep integration with telegraf, I will most likely also benefit from the internals being fully typed.

I know that technically this would all work without TS in the first place, but if it actually ends up being as generic as I'm contemplating, then I don't want to think about all the bugs I introduce when I don't have full type safety.

The main reason behind having awilix-telegraf is that larger bots almost inevitably end up combining a web server with a bot. Sooner or later, you almost certainly want to integrate with other APIs/OAuth/websites/you-name-it. As large web servers in Node.js are preferably using DI in some sort, it would be awesome to have telegraf integrate seamlessly with awilix.

I'm nitpicking at this point. Great work, I'll merge it after 24 hours, in case me, you or @dotcypress spots something wrong. Looking forward to reviewing your subsequent PRs :)

Thanks!

@wojpawlik wojpawlik merged commit e618f58 into telegraf:develop Aug 22, 2020
@foodornt
Copy link

foodornt commented Nov 1, 2020

Can I somehow use these typings in my project?

@KnorpelSenf
Copy link
Collaborator Author

@foodornt sure, check out typegram

@foodornt
Copy link

foodornt commented Nov 1, 2020

@KnorpelSenf thank you man.

btw, is there any wizardscene typings you know of?
or can I somehow use the develop branch to make my bot.

EDIT: I've done npm install https://github.com/telegraf/telegraf.git and I got some pretty good typings.
my neovim lsp got some errors when I tried to view these typings. adding the default tsconfig.ts file to the telegraf directory solved the issue (Execute the command tsc --init in that directory).

I lack a lot of knowledge in regards of relates to typescript, but is it right that I can't use ctx.wizard when adding middlewares to the wizard scene?

If anybody has that issue too, let me know. I checked the wizard scene ts file, and it looked alright. I mean there's an implemented middleware object that's doing something to use the wizard context but for some reason it's not working. is that a bug or am I missing out on something?

@KnorpelSenf
Copy link
Collaborator Author

KnorpelSenf commented Nov 1, 2020

Even though I migrated the module, I'm not an expert on wizards, so I cannot really help you here.

Feel free to open an issue and remember to include an SSCCE if possible. People won't necessarily read an old PR that's already been merged some time ago.

@foodornt
Copy link

foodornt commented Nov 2, 2020

@KnorpelSenf I think I worked it out by changing some types .
Much appreciated, thank you for helping!

This was referenced Nov 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extra::caption ovewrties itself TypeScript for Wizard scene
3 participants