Typescript issue
See original GitHub issueHello,
I am having an issue when trying to use the library with typescript in NestJS. I have the following code :
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import Mailjet, { SendEmailV3_1 } from 'node-mailjet';
@Injectable()
export class MailjetService {
private mailjet: Mailjet;
constructor(private readonly configService: ConfigService) {
this.mailjet = new Mailjet({
apiKey: this.configService.get('MJ_APIKEY_PUBLIC'),
apiSecret: this.configService.get('MJ_APIKEY_PRIVATE'),
});
}
async sendForgottenPasswordMail(token: string, recipientMail: string) {
const data: SendEmailV3_1.IBody = {
Messages: [
{
From: {
Email: 'some@email.com',
Name: 'SomeName',
},
To: [{ Email: recipientMail }],
TemplateID: SOME_TEMPLATE_ID,
},
],
};
const res = await this.mailjet
.post('send', { version: 'v3.1' })
.request<SendEmailV3_1.IResponse>(data);
const { Status } = res.body.Messages[0];
}
}
Typescript says :
Type 'IResponse' does not satisfy the constraint 'TRequestData'.
Type 'IResponse' is not assignable to type 'TUnknownRec'.
Index signature for type 'string' is missing in type 'IResponse'.ts(2344)
I can’t see any difference between my current code here and the documentation example, so any help is welcome. I think I’m probably missing something here.
package.json
version : 5.1.1
Bests, Max
Issue Analytics
- State:
- Created a year ago
- Reactions:21
- Comments:21
Top Results From Across the Web
Issues · microsoft/TypeScript - GitHub
TypeScript is a superset of JavaScript that compiles to clean JavaScript output. - Issues · microsoft/TypeScript.
Read more >Documentation - TypeScript 3.9
TypeScript 3.9 addresses this issue by changing the internals of how the compiler and language service caches file lookups.
Read more >7 really good reasons not to use TypeScript - everyday.codes
Everyone loves TypeScript. It “solves” many problems JS has, it is a “superset” of JS, it will make your code error-prone and pleasant...
Read more >Common TypeScript module problems and how to solve them
Common TypeScript module problems and how to solve them · Introduction · Problem 1: Irregular location of dependencies · Solution 1: Locate the ......
Read more >Pros and Cons of TypeScript - AltexSoft
TypeScript cons: what problems it creates · Not true static typing · One more JavaScript to learn · Bloated code · Adding extra...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m having an issue just trying to get it instantiated.
Error output says:
TypeError: node_mailjet_1.default is not a constructor
Am I missing something?
I think that the following code is fundamentally flawed:
The type of
result
should not representTRequestData
- that’s a request shape, not a response shape. You actually want it to beILibraryResponse<SendEmailV3_1.IResponse>
but it isn’t compatible.A workaround is to use a type predicate:
Arguably, this is more reliable than if the type system worked today. At least in this code, there’s runtime checking of the response to give you some level of confidence that the API response is what you expect. Feel free to extend
isSendResponse
with further checks for what you need (maybe a json schema check?)