鸿蒙OS应用开发之——api升级到10问题及解决办法

一 问题概述

  1. 'params' is possibly 'undefined'. <ArkTSCheck>
  2. Not all code paths return a value. <ArkTSCheck>
  3. Use explicit types instead of "any", "unknown" (arkts-no-any-unknown)
  4. Argument of type 'Tag[]' is not assignable to parameter of type 'string'.
  5. Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)
  6. Array literals must contain elements of only inferrable types (arkts-no-noninferrable-arr-literals) <ArkTSCheck>
  7. Type 'null' is not assignable to type 'UserData'. <ArkTSCheck>
  8. try catch报错
  9. 传值null问题
  10. Property 'searchEvent' has no initializer and is not definitely assigned in the constructor

二 问题解决

2.1 'params' is possibly 'undefined'. <ArkTSCheck>

1-api10之前

1
2
3
4
function httpRequest(url: string, method: http.RequestMethod, params?: object): Promise<ResponseResult>

AppStorage.Set(Constants.Cookie, `loginUserName_wanandroid_com=${params['username']};` + value.header['set-cookie'])
}

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
2
3
4
5
6
7
8
async getStringData(key: string) {
Logger.info(TAG, `Get begin`)
if (preferenceTheme !== null) {
let data: string = ''
data = await preferenceTheme.get(key, '') as string
return data;
}
}

2-api10之后

1
2
3
4
5
6
7
8
9
10
async getStringData(key: string) {
Logger.info(TAG, `Get begin`)
if (preferenceTheme !== null) {
let data: string = ''
data = await preferenceTheme.get(key, '') as string
return data;
}else{
return ''
}
}

说明:增加else返回值

2.3 Use explicit types instead of "any", "unknown" (arkts-no-any-unknown)

1-api10之前

1
2
3
4
const isEmpty = (data) => {
return data == undefined || data == "" || data.length <= 0 || data === '0'

};

2-api10之后

1
2
3
4
const isEmpty = (data:string) => {
return data == undefined || data == "" || data.length <= 0 || data === '0'

};

说明:需要注明数据类型,ArkTS不支持anyunknown类型

2.4 Argument of type 'Tag[]' is not assignable to parameter of type 'string'.

1-api10之前

1
2
3
4
const isEmpty = (data) => {
return data == undefined || data == "" || data.length <= 0 || data === '0'

};

2-api10之后

1
2
3
4
const isEmpty = (data:string|[]|number) => {
return data == undefined || data == "" || (data as []).length <= 0 || data === '0'

};

2.5 Object literal must correspond to some explicitly

1-api10之前

1
2
3
4
5
6
interface MyOptions {
options?: Object; // options 可以是任何非原始类型的对象, {} 或字典对象
}
const myOptions: MyOptions = {
options: { key1: 'value1', key2: 42 } // options 是一个具有属性的对象
};

2-api10之后

1
2
3
const myOptions: MyOptions = {
options: Object({ key1: 'value1', key2: 42 }) // options 是一个具有属性的对象
};

2.6 Array literals must contain elements of only inferrable types

1-api10之前

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
gridData= [
{
name: "工具",
backgroundColor: Color.Red
},
{
name: "问答",
backgroundColor: Color.Orange
},
{
name: "消息",
backgroundColor: Color.Yellow
},
{
name: "课程",
backgroundColor: Color.Green
},
{
name: "待办清单",
backgroundColor: Color.Pink
},
{
name: "分享文章",
backgroundColor: Color.Grey
},
]

2-api10之后

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
1-声明数据类型
interface FuncInterface {
'name': string;
'backgroundColor': Color;
}
2-定义数据
gridData:FuncInterface[]= [
{
name: "工具",
backgroundColor: Color.Red
},
{
name: "问答",
backgroundColor: Color.Orange
},
{
name: "消息",
backgroundColor: Color.Yellow
},
{
name: "课程",
backgroundColor: Color.Green
},
{
name: "待办清单",
backgroundColor: Color.Pink
},
{
name: "分享文章",
backgroundColor: Color.Grey
},
]

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
2
3
catch(err  => {

})

2-api10之后

1
2
3
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
2
3
4
5
export default struct BookDefaultItem {
item: Book
searchEvent: (event: ClickEvent) => void
moreEvent: (event: ClickEvent) => void
}

2-api10之后

1
2
3
4
5
export default struct BookDefaultItem {
item: Book = new Book()
searchEvent: (event: ClickEvent) => void = ()=>{}
moreEvent: (event: ClickEvent) => void = ()=>{}
}

三 参考

  • Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals) <ArkTSChec
  • 从TypeScript到ArkTS的适配规则