【发布时间】:2021-06-06 23:47:28
【问题描述】:
我正在尝试从 Node 迁移到 Typescript 进行调整,所以我设置了一堆 eslint 来帮助我:
我似乎无法理解以下 eslint 错误:
函数缺少返回类型。 eslint@typescript-eslint/explicit-module-boundary-types
这是我的代码,错误是在 sns 函数上引发的。
const sns = ({config, logger}: SNSInterface) => {
const client = new SNSClient({endpoint: config.sns.endpoint});
const publish = async ({message, topicArn}: PublishInterface) : Promise<void> => {
const command = new PublishCommand({
Message: JSON.stringify(message),
TopicArn: topicArn,
});
logger.debug({message, topicArn}, 'Publishing SNS Message');
const data = await client.send(command).catch((err) => {
logger.error(err, 'Failed to send Topic ARN');
throw err;
});
logger.debug({data}, 'Publishing SNS Message Results');
};
return {
publish,
};
};
export default sns;
虽然我可以通过添加来解决它:
interface SNSReturnInterface {
publish: ({ message, topicArn }: PublishInterface) => Promise<void>;
}
const sns = ({config, logger}: SNSInterface): SNSReturnInterface => {
这真的是最好的方法吗?我认为 Typescript 应该可以推断这一点?如果我想向发布函数添加一个参数,那是否意味着我每次都必须更改 ReturnInterface 定义?除非必要,我不想关闭规则。感谢任何建议!
【问题讨论】:
标签: typescript typescript-eslint