Reputation: 770
I don't know how to set the correct topic for my question, so if someone has a better name, please edit me :)
The subject is:
I have 2 functions that use the same interface:
interface createMailInterface {
to: String,
subject: String,
message: String
}
const createMail = (config: createMailInterface) => {
const {to, subject, message} = config
const mailOptions = {
from: process.env.MAIL_USER_NAME,
to,
subject,
message
}
return mailOptions
}
const sendMail = (config: createMailInterface): void => {
transporter.sendMail( createMail(config), (error, info) => {
if (error) {
logger.error(error)
} else {
logger.info(`Email sent: ${info.response}`)
}
})
}
But in declaration on sendMail
function I have a warning like:
Argument of type '{ from: string | undefined; to: String; subject: String; message: String; }' is not assignable to parameter of type 'Options'.
in place: createMail(config)
So my question is, how can I use the same interface for nested functions like in the example above?
Upvotes: 0
Views: 281
Reputation: 189
It looks like your transporter.sendMail
method has it's own interface defined called 'Options'. You can see the type definition by command/control clicking on the transporter.sendMail
method.
I'm guessing the interface is something like this.
interface Options {
from: string
to: string
subject: string
}
In your case, your "from" variable can be string or undefined because process.env.MAIL_USER_NAME
can theoretically be undefined, so you can have an
if(process.env.MAIL_USER_NAME){
** Run code here **
}
or an
if(!process.env.MAIL_USER_NAME){
** Handle your error **
}
To make sure it is never undefined, for the "to" and "subject" variables, just change your "String" in your interface to "string".
Upvotes: 1