2021年3月4日星期四

javascript - parameter one of two possible values

Is there a way, in javascript (no typescript), to specify that the parameter of a method has to be "one of" [value1, value2]?

For example, if I have a function:

const handleCommentAction = (action) => {      if (action === "add") {          setTotalComments(totalComments + 1);      } else if (action === "delete") {          setTotalComments(totalComments - 1);      }  }  

if there any way to specify that action has to be one of ["add", "delete"] ?

// Something like this...  const handleCommentAction = (action: ["add", "delete"]) => {  

or is impossible?

https://stackoverflow.com/questions/66485711/javascript-parameter-one-of-two-possible-values March 05, 2021 at 10:03AM

没有评论:

发表评论