鸿蒙OS应用开发之——api升级到10问题及解决办法
一 问题概述
- 'params' is possibly 'undefined'. <ArkTSCheck>
- Not all code paths return a value. <ArkTSCheck>
- Use explicit types instead of "any", "unknown" (arkts-no-any-unknown)
- Argument of type 'Tag[]' is not assignable to parameter of type 'string'.
- Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)
- Array literals must contain elements of only inferrable types (arkts-no-noninferrable-arr-literals) <ArkTSCheck>
- Type 'null' is not assignable to type 'UserData'. <ArkTSCheck>
- try catch报错
- 传值null问题
- Property 'searchEvent' has no initializer and is not definitely assigned in the constructor
二 问题解决
2.1 'params' is possibly 'undefined'. <ArkTSCheck>
1-api10之前
1 | function httpRequest(url: string, method: http.RequestMethod, params?: object): Promise<ResponseResult> |
2-api10之后
1 | AppStorage.Set(Constants.Cookie, `loginUserName_wanandroid_com=${params?['username']:''};` + value.header['set-cookie']) |
说明:params需要判空
2.2 Not all code paths return a value. <ArkTSCheck>
1-api10之前
1 | async getStringData(key: string) { |
2-api10之后
1 | async getStringData(key: string) { |
说明:增加else返回值
2.3 Use explicit types instead of "any", "unknown" (arkts-no-any-unknown)
1-api10之前
1 | const isEmpty = (data) => { |
2-api10之后
1 | const isEmpty = (data:string) => { |
说明:需要注明数据类型,ArkTS不支持any
和unknown
类型
2.4 Argument of type 'Tag[]' is not assignable to parameter of type 'string'.
1-api10之前
1 | const isEmpty = (data) => { |
2-api10之后
1 | const isEmpty = (data:string|[]|number) => { |
2.5 Object literal must correspond to some explicitly
1-api10之前
1 | interface MyOptions { |
2-api10之后
1 | const myOptions: MyOptions = { |
2.6 Array literals must contain elements of only inferrable types
1-api10之前
1 | gridData= [ |
2-api10之后
1 | 1-声明数据类型 |
2.7 Type 'null' is not assignable to type 'UserData'
1-api10之前
1 | @State userData: UserData = null |
2-api10之后
1 | @State userData: UserData = new UserData() |
2.8 try catch报错
1-api10之前
1 | catch(err => { |
2-api10之后
1 | catch((err:Error) => { |
2.9 传值null问题
1-api10之前
1 | new SettingItem($r('app.string.setting_list_theme'), $r('app.media.ic_theme'), null) |
2-api10之后
1 | new SettingItem($r('app.string.setting_list_theme'), $r('app.media.ic_theme'), $r(null)) |
2.10 Property 'searchEvent' has no initializer and is not definitely assigned in the constructor
1-api10之前
1 | export default struct BookDefaultItem { |
2-api10之后
1 | export default struct BookDefaultItem { |
三 参考
- Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals) <ArkTSChec
- 从TypeScript到ArkTS的适配规则