Javascript 重载Typescript arrow函数而不会导致错误重复标识符,并且函数实现丢失或没有立即执行
我们使用以下代码为我们的redux还原程序启用编译检查和IntelliSense: 它位于Javascript 重载Typescript arrow函数而不会导致错误重复标识符,并且函数实现丢失或没有立即执行,javascript,reactjs,typescript,redux,eslint,Javascript,Reactjs,Typescript,Redux,Eslint,我们使用以下代码为我们的redux还原程序启用编译检查和IntelliSense: 它位于.ts文件中,而不是.tsx() 这意味着我们得到了以下脱毛错误: 7:8 error Expected a function expression func-style 7:8 error Use const or class constructors instead of named functions prefer-arr
.ts
文件中,而不是.tsx
()
这意味着我们得到了以下脱毛错误:
7:8 error Expected a function expression func-style
7:8 error Use const or class constructors instead of named functions prefer-arrow/prefer-arrow-functions
如果将typedAction
功能转换为箭头功能,则会收到以下错误:
export const typedAction = (type: string, payload?: any) => {
return { type, payload };
}
TS2300(TS)重复标识符“typedAction”
TS2391(TS)功能执行缺失或未立即执行
声明之后
为了能够继续下去,可以这样解决,但更持久的解决方案更可取:
export function typedAction<T extends string>(type: T): { type: T };
export function typedAction<T extends string, P extends any>(
type: T,
payload: P
): { type: T; payload: P };
// eslint-disable-next-line func-style, prefer-arrow/prefer-arrow-functions
export function typedAction(type: string, payload?: any) {
return { type, payload };
}
导出函数typedAction(type:T):{type:T};
导出函数类型化(
类型:T,
有效载荷:P
):{type:T;有效载荷:P};
//eslint禁用下一行func样式,首选箭头/首选箭头函数
导出函数类型操作(类型:字符串,有效负载?:任意){
返回{type,payload};
}
该规则不合适。命名函数适用于重载。该规则不适用。命名函数适用于重载。
7:8 error Expected a function expression func-style
7:8 error Use const or class constructors instead of named functions prefer-arrow/prefer-arrow-functions
export const typedAction = (type: string, payload?: any) => {
return { type, payload };
}
export function typedAction<T extends string>(type: T): { type: T };
export function typedAction<T extends string, P extends any>(
type: T,
payload: P
): { type: T; payload: P };
// eslint-disable-next-line func-style, prefer-arrow/prefer-arrow-functions
export function typedAction(type: string, payload?: any) {
return { type, payload };
}