Reactjs 如何修复“预期调用签名具有typedef”错误
我在render JSX元素中有一个switch case函数,我在另一个基于返回值的常量值中使用它 我试着在网上寻找我的问题的解决方案,但我找到的答案没有一个对我的问题有帮助,即使是在我的代码上尝试过Reactjs 如何修复“预期调用签名具有typedef”错误,reactjs,typescript,Reactjs,Typescript,我在render JSX元素中有一个switch case函数,我在另一个基于返回值的常量值中使用它 我试着在网上寻找我的问题的解决方案,但我找到的答案没有一个对我的问题有帮助,即使是在我的代码上尝试过 function carouselClass(transitionTypeValue: any) { switch(transitionTypeValue) { //Different cases and their return valu
function carouselClass(transitionTypeValue: any) {
switch(transitionTypeValue) {
//Different cases and their return values
}
}
const classes = xyz(carouselClass(transitionType)) //Sample of where I'm using my switch function
代码在Typescript端运行良好,但我得到一个linting错误,表示typedef期望调用签名:“carouselClass”具有typedef
我尽了最大努力提供足够的上下文,请告诉我是否需要更多,因为这是我第一次发布问题。Linter给出“缺少类型定义”警告。看看这个- 详情请参阅 解决方案:您需要显式指定函数的返回类型
function carouselClass(transitionTypeValue: any):any {
switch(transitionTypeValue) {
//Different cases and their return values
}
}
我认为如果您正在调用函数并返回某些内容,则应该添加返回类型
function carouselClass(transitionTypeValue: any):any {
switch(transitionTypeValue) {
//Different cases and their return values
}
}
const classes = xyz(carouselClass(transitionType))
您应该尽可能避免使用任何类型。避免使用多个类型返回任何或值, 但如果您仍然希望返回多种类型的值,请尝试查找相似性并创建枚举类型或包装器接口,或者您也可以尝试创建泛型函数
interface IClassType extend IClassA, IClassB {}
function carouselClass(transitionTypeValue: any): IClassType {
switch(transitionTypeValue) {
// Different cases and their return values
// Avoid returning multiple types,
// but if you still want to return multiple types of value,
// try to find similarity and create Enum type or Wrapper Interface
}
}
const classes: xyz<IClassType> = xyz(carouselClass(transitionType))
void会比任何东西都好吗?除非你不返回任何东西,否则你不能使用void。