HarmonyOS 鸿蒙Next myViewModel 更新视图报 Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params) 错误
HarmonyOS 鸿蒙Next myViewModel 更新视图报 Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params) 错误
基础BaseViewModel代码如下:
import useRequestStatus from '../http/useRequestStatus'
import {LoadStateResult} from '../http/useRequestStatus'
export type StateCallback = (state: string) => void
// 使用泛型定义 ResultCallback
export type ResultCallback<T> = (result: T) => void;
export class BaseViewModel {
private stateCallback?: StateCallback
observeState(stateCallback: StateCallback) {
this.stateCallback = stateCallback
}
dispatchViewState(state: string) {
this.stateCallback?.(state)
}
httpRequest<D>(checkResult?: boolean): LoadStateResult<D> {
return useRequestStatus(
(state: string) => {
this.dispatchViewState(state);
}, checkResult);
}
}
更新状态类:
import { ViewStateConstant } from '../common/ViewStateConstant';
import { AxiosError } from "@ohos/axios"
import axios from "@ohos/axios"
import { Logger } from '../common/utils/Logger';
// 简化 LoadStateResult 接口定义
export interface LoadStateResult<D> {
promise: (requestPromise: Promise<D>, showLoadingState?: boolean) => Promise<D>;
}
type ViewStateUpdate = (state: string) => void;
export interface ApiResult<D>{
// code: number;
// msg: string;
data: D ;
}
// 定义updateViewState函数
function updateViewState(stateCallback: ViewStateUpdate, state: string = ViewStateConstant.VIEW_STATE_SUCCESS) {
stateCallback?.(state);
}
// run 函数
function run<D>(stateCallback: ViewStateUpdate, requestPromise: Promise<D>, showLoadingState?: boolean): Promise<D> {
return new Promise((resolve, reject) => {
if (showLoadingState !== false) {
updateViewState(stateCallback as ViewStateUpdate, ViewStateConstant.VIEW_STATE_LOADING);
}
requestPromise
.then(res => {
Logger.debug('状态更新完毕');
updateViewState(stateCallback, ViewStateConstant.VIEW_STATE_SUCCESS);
resolve(res);
})
.catch(error => {
if (axios.isAxiosError(error)) {
updateViewState(stateCallback, ViewStateConstant.VIEW_STATE_NETWORK_ERROR);
} else {
updateViewState(stateCallback, ViewStateConstant.VIEW_STATE_ERROR);
}
reject(error);
});
});
}
const useRequestStatus = <D>(stateCallback: ViewStateUpdate, checkResult: boolean = false): LoadStateResult<D> => {
return {
promise: (requestPromise: Promise<D>, showLoadingState?: boolean) => {
return run(stateCallback, requestPromise, showLoadingState);
}
} as LoadStateResult<D>;
};
export default useRequestStatus;
更多关于HarmonyOS 鸿蒙Next myViewModel 更新视图报 Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params) 错误的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
(arkts-no-inferred-generic-params) 表明在调用泛型函数时,ArkTS 编译器无法自动推断出正确的类型参数。
我这边也用axios封装了一下,可以参考:
https://developer.huawei.com/consumer/cn/blog/topic/03151854787432021
更多关于HarmonyOS 鸿蒙Next myViewModel 更新视图报 Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params) 错误的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
楼主您好,报错提示是需要显式标注泛型函数类型实参。可以参考如下链接进行修改。
从TypeScript到ArkTS的适配规则-从TypeScript到ArkTS的适配指导-学习ArkTS语言-基础入门 | 华为开发者联盟 (huawei.com)
await this.httpRequest()这里报Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params) 错误 请大佬帮忙看一下
针对HarmonyOS鸿蒙系统中遇到的“Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)”错误,这通常是由于在调用泛型函数时,编译器无法正确推断出泛型的具体类型导致的。
在鸿蒙开发中,尤其是使用ArkTS(ArkUI的TypeScript声明式开发框架)时,确保泛型函数的调用中明确指定了泛型参数的类型是非常重要的。这种错误可能发生在多种情况下,比如在使用组件的某些方法或者自定义泛型函数时,如果未正确指定泛型参数,编译器就会抛出此类错误。
解决方法:
- 检查引发错误的泛型函数调用,确保在调用时明确指定了泛型参数的类型。
- 如果是在自定义泛型函数或方法中遇到此问题,尝试在定义时添加类型约束,或者在调用时提供足够的信息来帮助编译器进行类型推断。
如果在进行上述检查后问题依旧存在,可能是由于鸿蒙系统的某个特定版本或工具链中的已知问题。此时,建议查阅最新的鸿蒙开发文档或更新到最新的开发环境,以确认是否有相关的修复或更新。
如果问题依旧没法解决请联系官网客服, 官网地址是 https://www.itying.com/category-93-b0.html,