F
F
ForsakenHedgehog2019-12-02 12:20:48
typescript
ForsakenHedgehog, 2019-12-02 12:20:48

How to type data coming via api in Typescript?

I don't understand typing in some cases.
There is a node_telegram_bot_api, I decided to practice with typescript on it (previously I worked very little with typed languages, only we solve all sorts of problems at school in C++).
When I create variables or objects/arrays myself, I know what data type they should be.
When I use data that comes from outside, I fall into a stupor.
Stupor places in comments inside the code:

const TelegramBot = require('node_telegram_bot_api');

const bot: /*  нужно ли обозначать тип в данном случае? */ = new TelegramBot(TOKEN, {  
    polling: true
})

bot.on("message", (msg: /* Сюда приходит объект с разной глубиной вложенности данных.
Тип object с пречислением всех возможных данных { ... } - не кажется правильным, хотя я может быть и ошибаюсь*/) => {
    bot.sendMessage(msg.chat.id, "Hello world")!
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RidgeA, 2019-12-02
@ForsakenHedgehog

you can describe the interface and specify that msg corresponds to a specific interface

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question