Skip to content

Conversation

KnorpelSenf
Copy link
Collaborator

@KnorpelSenf KnorpelSenf commented Nov 27, 2020

Description

The types of Stages, Scenes, and Wizards were mostly wrong. The examples did not typecheck.

This PR applies consistent types to this part of the library. The example bots are migrated to TS, they build now with minimal changes.

While the types should be correct everywhere now, this comes with the downside of complexity. Scenes rely on the session. As a result, everything is bloated up to having two type variables: one for the session data and one for the context object, as both types can be adjusted. This trickles down far.

Note that the type variables follow the standard convention of single character names rather than the special telegraf version of prepending a T. We can change this back if desired, I simply found it even less readable than the current version to make all names longer.

Fixes #1172
Fixes #1165
Fixes #894

Helps with

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update (need an example bot that shows how to customize context and session data)

How Has This Been Tested?

npm t
npm run build
npm run lint

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • 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.

I don't see anything obviously very wrong, but I understand pretty much nothing here...

@KnorpelSenf
Copy link
Collaborator Author

KnorpelSenf commented Nov 27, 2020

but I understand pretty much nothing here

Then check out what is going on in #1195 … no but seriously, do you think that I should write more comments?

@wojpawlik
Copy link
Member

I don't know, better ask someone familiar with Scenes/Stages/Wizards

@KnorpelSenf
Copy link
Collaborator Author

I don't know, better ask someone familiar with Scenes/Stages/Wizards

Who would that be?

I also had no idea about these things before I started working on this PR, so I'd appreciate it if someone with more experience than me could have a look.

@KnorpelSenf
Copy link
Collaborator Author

@dotcypress git blame told me that you an expert on this domain—would you mind reviewing this PR?

@wojpawlik wojpawlik added this to the v4 milestone Nov 28, 2020
@wojpawlik wojpawlik mentioned this pull request Nov 29, 2020
26 tasks
This was referenced Nov 30, 2020
@wojpawlik wojpawlik requested a review from dotcypress December 1, 2020 15:24
@wojpawlik wojpawlik marked this pull request as draft December 2, 2020 14:46
@wojpawlik wojpawlik mentioned this pull request Dec 5, 2020
6 tasks
@KnorpelSenf KnorpelSenf marked this pull request as ready for review December 8, 2020 21:38
@KnorpelSenf
Copy link
Collaborator Author

KnorpelSenf commented Dec 8, 2020

@IMalyugin @wwwat @foodornt you all seem to be interested in having TypeScript types for Stages/Scenes.

The types are fixed now. However, I have never used that feature of telegraf, so I have quite little experience with scenes etc. Can you please pull in the code from this branch and verify that

  • the types work as expected,
  • the PR does not alter the behaviour of the feature, and
  • you can customise the types with your own session data and context objects in order to adjust them to your needs?

This would be very helpful. We cannot merge this without other people's approval :)

Regarding the third point: I will write (UPDATE: done) some examples about how to that. This is necessary anyway, but please don't hesitate to validate the first two things already!

@KnorpelSenf KnorpelSenf marked this pull request as draft December 8, 2020 23:28
@KnorpelSenf
Copy link
Collaborator Author

Some type variables are redundant. It has to be possible to infer certain variables across session, scene.session, and session.__scenes. It is annoying to type the names multiple times, and also it does not make sense to specify them differently in different positions.

This needs to be figured out before a merge.

@KnorpelSenf KnorpelSenf marked this pull request as ready for review December 9, 2020 15:56
@KnorpelSenf
Copy link
Collaborator Author

KnorpelSenf commented Dec 9, 2020

I added a lot of example bots. They illustrate every possible way to use the type variables. It's mostly redundant stuff, so it should be enough to check out the most basic one and the most advanced one for reviewing this PR.

There is a lot of nesting of the same objects for scenes and sessions and sessions in scenes and so on. This makes it very convenient to use, so I don't think we should change it, but it also makes the types a bit annoying internally. They are pretty much fine on the surface, though.

@KnorpelSenf
Copy link
Collaborator Author

KnorpelSenf commented Dec 10, 2020

I figured out #1194 (comment).

The problem is that ctx.scene.session is an alias for ctx.session.__scenes. It is in fact possible to add type annotations to ctx.scene: SceneContextScene that infer this correctly, however, as soon as we extend scenes and add wizards, this logic breaks. It is not possible to keep up this alias without larger restructurings of the module, which I'd like to avoid.

As a consequence, whenever the scene session under ctx.scene.session is augmented, this type has to be specified twice, once in a place that is responsible for ctx.scene.session and once for ctx.session.__scenes. I added a large number of examples that illustrate this.

It is difficult to explain to new users why type inference works correctly for scenes but not for wizards in this place. Hence, it makes sense to simply advise to consequently specify both type variables for SceneContext and WizardContext whenever the scene/wizard-specific session is extended. Nothing can go wrong here, it may just be redundant in very special cases that are too rare to care about.

@wojpawlik wojpawlik mentioned this pull request Dec 10, 2020
@wojpawlik
Copy link
Member

Please review and test #1224, after it's merged you can merge develop here to fix the conflict and // Enable graceful stop to all the new examples. I'll review this in the meantime.

@wojpawlik
Copy link
Member

I'd still be happy to get review from someone more experienced with the stuff before I approve.

@KnorpelSenf
Copy link
Collaborator Author

KnorpelSenf commented Dec 11, 2020

@wojpawlik For reviewing this, it helps a lot to install ts-node and run the example bots. You can

bot.use(async (c,n) => { console.log(c.session); await n(); console.log(c.session); })

to inspect that the scenes actually do their job.

#705 also helps a lot, that's what I read before getting into it.

The types are easy to check by verifying that the scenes-bot-custom-context-and-session-and-scene-and-scene-session.ts compiles, which it does.

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.

It'd be great if you sorted these out:

 /home/runner/work/telegraf/telegraf/src/scenes/context.ts
Warning:   101:10  warning  Unexpected nullable object value in conditional. An explicit null check is required  @typescript-eslint/strict-boolean-expressions
Warning:   120:10  warning  Unexpected nullable object value in conditional. An explicit null check is required  @typescript-eslint/strict-boolean-expressions

/home/runner/work/telegraf/telegraf/src/stage.ts
Warning:   27:12  warning  Unexpected string value in conditional. An explicit empty string check is required  @typescript-eslint/strict-boolean-expressions

Other than that, I don't see anything wrong here, but to be honest I understand almost nothing, so I just trust TypeScript (and you).

@KnorpelSenf
Copy link
Collaborator Author

If no one else shows up and pops a question until the end of tomorrow, I'll merge.

I have tested example bots for both scenes and wizards thoroughly and they worked without problems. Writing the other example bots made me confident that the types are correct.

Even if we missed something here, the existing types will be an order of magnitude better than the current state, so the rest is fixing bugs (and I can do that).

@KnorpelSenf KnorpelSenf changed the title Fix scene types Types and examples for scenes and wizards Dec 12, 2020
@KnorpelSenf KnorpelSenf merged commit 352dd5d into develop Dec 12, 2020
@KnorpelSenf KnorpelSenf deleted the fix-scene-types branch December 12, 2020 16:40
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.

Type defenition of ctx.scene.state Typescript building error Wrong typings in 3.38.0
2 participants