I have this code and I recieve an error from Typescript regarding res 'Argument of type…
- User
I have this code and I recieve an error from Typescript regarding res 'Argument of type 'IMessage' is not assignable to parameter of type 'never'.' How do I fix it? const sendMessages = async ( prepareMessages: ISendedMessage[], ): Promise<IMessage[]> => { const result = []; for (let i = 0; i < prepareMessages.length; i++) { try { const res = await sendSingleMessage(prepareMessages[i]); result.push(res); } catch (error) { console.error('Ошибка при отправке сообщения'); break; } } return result; };
- Assistant
The error message "Argument of type 'IMessage' is not assignable to parameter of type 'never'" suggests that TypeScript is unable to infer the type of the
resvariable in thesendMessagesfunction.To fix this issue, you can explicitly define the type of
resasIMessagein thesendMessagesfunction. You can do this by adding a type annotation to theresvariable declaration like this:typescriptconst sendMessages = async ( prepareMessages: ISendedMessage[], ): Promise<IMessage[]> => { const result: IMessage[] = []; for (let i = 0; i < prepareMessages.length; i++) { try { const res: IMessage = await sendSingleMessage(prepareMessages[i]); result.push(res); } catch (error) { console.error('Ошибка при отправке сообщения'); break; } } return result; };By explicitly defining the type of
resasIMessage, TypeScript should no longer raise the error and the function should work as expected. - User
It didn't change anything. I still get this error. What else can I do?
- Assistant
In this case, you can try to specify the return type of the
sendSingleMessagefunction to explicitly tell TypeScript that it returns a promise of typeIMessage.You can define the return type of
sendSingleMessagelike this:typescriptconst sendSingleMessage = async (message: ISendedMessage): Promise<IMessage> => { // Function implementation };By explicitly specifying the return type of
sendSingleMessageasPromise<IMessage>, TypeScript should be able to infer the type of theresvariable correctly in thesendMessagesfunction. This should resolve the error you are seeing.After making this change, the
sendMessagesfunction should work without any issues. - User
I have this React code return ( <DialogMessage key={message.id} messageId={message.id} quoteId={message.replyId} quoteText={message.replyText} side={message.clientId != null ? 'left' : 'right'} infoText={getMessageSignature({ date: +message.date * 1000, channelName: message.channel?.name, user: message.user, isClientMessage: Boolean(message.clientId), })} highlight={message.id === scrollMessageId} onHighlightEnd={handleHighlightEnd} onReplyButtonClick={onReplyButtonClick} onQuoteClick={handleQuoteClick} avatar={message.user?.photo ?? null} > <a href={
${domainStore.$domain.getState()}/storage/files/${getMediaUrl( message.mediaUrl, )}} > Ссылка на файл and I have a typescript error on message.mediaUrl 'Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'.' How can I fix it if I know that mediaUrl can be undefined as well as defined? - Assistant
To fix the TypeScript error in this case, you can handle the case where
message.mediaUrlisundefinedbefore passing it to thegetMediaUrlfunction. You can use a conditional check to provide a default value (such as an empty string) whenmessage.mediaUrlisundefined.Here's how you can modify the code to handle this scenario:
typescript<a href={`${domainStore.$domain.getState()}/storage/files/${getMediaUrl( message.mediaUrl ?? '', // Use default empty string if mediaUrl is undefined )}`} > Ссылка на файл </a>By using the nullish coalescing operator (
??) to provide a default empty string whenmessage.mediaUrlisundefined, you ensure that the argument passed togetMediaUrlis always of typestringas required. This way, TypeScript should no longer raise the error related to the parameter type mismatch.