airgram icon indicating copy to clipboard operation
airgram copied to clipboard

MessageContentUnion is empty

Open Fxlr8 opened this issue 5 years ago • 5 comments

Issue type:

[ ] bug report

Airgram version:

[ ] latest

image

Hi, looks like message.content type has no properties. I think this happens because currently it is a union of all possible types.

MessageContentUnion = MessageText | MessageAnimation | MessageAudio...

I think it should be an aggregate of all possible types.

MessageContentUnion = MessageText & MessageAnimation & MessageAudio...

Fxlr8 avatar Jul 19 '20 12:07 Fxlr8

Hello, I believe current behavior is correct. You can use chaining operator to avoid the error. Like:

const text = update.message.content?.text

sdcdsvsdv423 avatar Jul 24 '20 06:07 sdcdsvsdv423

Nope, looks like there is a problem with types. text property of content should be optional, but it does not exist.

image

Fxlr8 avatar Jul 27 '20 13:07 Fxlr8

The text property is not optional. Some content types (like messageText) always contain this property, while others never do. Different message types represent by different set of properties and almost all properties are "optional".

You need to check what kind of content you handle to avoid this error:

airgram.on('updateNewMessage', async (ctx) => {
      const { message } = ctx.update
      if (message.content._ === 'messageText') {
        console.log(message.content.text) // no error
      }
})

sdcdsvsdv423 avatar Jul 28 '20 05:07 sdcdsvsdv423

I did exactly what you said, but I still get the error. What version of typescript are you using?

image

I could make it work with a direct type cast, but it is not very convenient

const content = update.message.content as MessageContent.MessageText

Fxlr8 avatar Aug 01 '20 13:08 Fxlr8

I've tested with Typescript 3.7.5 and 3.9.7. Can you create a small repository to reproduce this error?

sdcdsvsdv423 avatar Aug 02 '20 02:08 sdcdsvsdv423