Skip to content

Conversation

wojpawlik
Copy link
Member

@wojpawlik wojpawlik commented Nov 11, 2020

#1074 (comment)

I decided that the best way to verify that examples are correct is to typecheck them. This revealed several typing issues, some of which I fixed.

Type of change

  • Documentation (typos, code examples or any documentation update)
  • Bug fix (non-breaking change which fixes an issue)

@wojpawlik wojpawlik force-pushed the typecheck-examples branch 3 times, most recently from 218a3e0 to 6f8d32f Compare November 12, 2020 14:54
@wojpawlik
Copy link
Member Author

Unsure what to do with the rest, please take over @KnorpelSenf

@KnorpelSenf
Copy link
Collaborator

Depends on KnorpelSenf/typegram#11.

@wojpawlik how is it even possible that sending files works? We're passing { "url": "http://example.com/file", "filename": "my-file" } to the Telegram servers. Where does it say that it works that way? The docs state that we can only pass a string as URL. What am I missing here?

Copy link
Collaborator

@KnorpelSenf KnorpelSenf left a comment

Choose a reason for hiding this comment

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

I added scripts to package.json that make it simple to build the docs.

All typing errors are resolved, except in two areas: scenes and wizards. It looks to me like the library itself needs some tweaks here before the examples can work.

@@ -13,9 +12,9 @@ const invoice = {
{ label: 'Working Time Machine', amount: 4200 },
{ label: 'Gift wrapping', amount: 1000 }
],
payload: {
payload: JSON.stringify({
Copy link
Collaborator

Choose a reason for hiding this comment

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

@wojpawlik can you confirm that this is correct?


const bot = new Telegraf(process.env.BOT_TOKEN)
bot.start(({ replyWithInvoice }) => replyWithInvoice(invoice))
bot.command('buy', ({ replyWithInvoice }) => replyWithInvoice(invoice, replyOptions))
bot.on('shipping_query', ({ answerShippingQuery }) => answerShippingQuery(true, shippingOptions))
bot.on('shipping_query', ({ answerShippingQuery }) => answerShippingQuery(true, shippingOptions, undefined))
Copy link
Collaborator

Choose a reason for hiding this comment

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

We may need to redefine that function's signature. It would be nice if undefined is not required here.

Copy link
Member Author

Choose a reason for hiding this comment

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

BY adding true and false overloads?

Copy link
Collaborator

Choose a reason for hiding this comment

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

How are you going to make true and false overloads for string | undefined?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nvm, you mean overloads for ok. Yes, that makes a lot of sense.

Comment on lines +19 to +31
export interface InputFileByPath {
source: string
}
export interface InputFileByReadableStream {
source: NodeJS.ReadableStream
}
export interface InputFileByBuffer {
source: Buffer
}
export interface InputFileByURL {
url: string
filename?: string
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this correct? I'm not sure if this accurately describes the InputFiles that telegraf can handle.

Copy link
Member Author

Choose a reason for hiding this comment

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


/** @deprecated */
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a partial redefinition of the arguments of sendInvoice in typegram. Should this be removed?

@@ -269,7 +269,7 @@ class Telegram extends ApiClient {
*/
sendSticker(
chatId: number | string,
sticker: tt.InputFile,
sticker: tt.Opts<'sendSticker'>['sticker'],
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is important to note here that string must not be part of InputFile. It is not possible to use a URL or a file_id when sending a sticker, and typegram already models this correctly. The correct type is simply InputFile as it is defined.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@@ -282,7 +282,7 @@ class Telegram extends ApiClient {
*/
sendVideo(
chatId: number | string,
video: tt.InputFile,
video: tt.Opts<'sendVideo'>['video'],
Copy link
Collaborator

Choose a reason for hiding this comment

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

typegram makes sure that string is included in this definition. Videos can be sent via file_id.

Comment on lines 350 to 358
const hasMedia = includesMedia(payload)
let buildConfig: Promise<RequestInit>
const configPayload: { [key: string]: unknown } = payload
if (hasMedia) {
configPayload[method] = method
buildConfig = buildFormDataConfig(configPayload, options.agent)
} else {
buildConfig = buildJSONConfig(payload)
}
Copy link
Member Author

Choose a reason for hiding this comment

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

...what does it improve?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It makes the code compile. I have no idea why it suddenly didn't anymore. Do let me know what the issue was in case you decide to investigate this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Type '{ method: M; } & Opts<M>' is not assignable to type '{ [key: string]: unknown; }'.ts(2322) was the error, perhaps related to https://devblogs.microsoft.com/typescript/announcing-typescript-3-5/#k-string-unknown-is-no-longer-a-wildcard-assignment-target

@wojpawlik wojpawlik marked this pull request as ready for review November 24, 2020 13:30
@wojpawlik
Copy link
Member Author

Everything's alright @KnorpelSenf? I'll merge this (without squashing) if yes, even tho last 2 examples aren't checked.

@wojpawlik wojpawlik merged commit d212681 into develop Nov 24, 2020
@wojpawlik wojpawlik deleted the typecheck-examples branch November 24, 2020 13:44
@wojpawlik wojpawlik mentioned this pull request Dec 1, 2020
26 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants