[英]How to disable warnings from @typescript-eslint?
Below is my code:下面是我的代码:
interface DeleteRecipeFunctionContext {
handleRecipeDelete: (id: string | number) => void;
}
const RecipeContext = createContext<DeleteRecipeFunctionContext>({
handleRecipeDelete: (id) => null, // eslint-disable-line no-unused-vars
});
createContext requires me to pass in a default function, so I am passing in a (id)=>null as a dummy placeholder before I pass in the actual function. createContext 要求我传入默认的 function,因此在传入实际的 function 之前,我将传入 (id)=>null 作为虚拟占位符。 I receive the warning from @typescript-eslint stating:
我收到来自@typescript-eslint 的警告,指出:
'id' is defined but never used @typescript-eslint/no-unused-vars
Writing // eslint-disable-warning/no-unused-vars
does not disable the warning, but eslint-disable-warning
does.写
// eslint-disable-warning/no-unused-vars
不会禁用警告,但eslint-disable-warning
会。 How should I turn off this warning without turning off all warnings?我应该如何在不关闭所有警告的情况下关闭此警告? On the other hand, is my approach of using createContext correct (to enter a dummy function)?
另一方面,我使用 createContext 的方法是否正确(输入虚拟函数)?
Disabling warnings should be a last resort option.禁用警告应该是最后的选择。 Couple of alternatives here:
这里有几个选择:
Discard the parameter:丢弃参数:
handleRecipeDelete: (_) => null
Omit the parameter:省略参数:
handleRecipeDelete: () => null
A more robust option is to make your context nullable.一个更强大的选择是使您的上下文可以为空。
const RecipeContext = createContext<DeleteRecipeFunctionContext | undefined>();
and do a null check when accessing your context, often done in a custom hook:并在访问您的上下文时执行 null 检查,通常在自定义挂钩中完成:
function useRecipeContext() { const context = useContext(RecipeContext); if (.context) { throw new Error("Missing RecipeContext;Provider value"); } return context; }
To selectively disable a warning generated by a plugin rule, use the syntax要选择性地禁用插件规则生成的警告,请使用语法
// eslint-disable-line <plugin>/<rule>
in your case:在你的情况下:
handleRecipeDelete: (id) => null, // eslint-disable-line @typescript-eslint/no-unused-vars
I do not know if your approach to creating a context is correct, that is something that you will need to test in your application.我不知道您创建上下文的方法是否正确,这是您需要在应用程序中测试的内容。
Above your function you can add // @ts-ignore
在您的 function 上方,您可以添加
// @ts-ignore
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.