TypeScript高级类型 在鸿蒙中的使用 Partial、Required、Readonly、Pick、Record

我的工程代码在这里,持续更新中
欢迎交流,谢谢
https://github.com/MartinLi89/WanHarmony

Partial <Type>

新定义 一个类型,将所有属性变为可选的类.

class TextTS {
  a: string = "1"
  b: string = "2"
  c: string = "3"
}

//1 Partial <Type>新定义 一个类型,将所有属性变为可选的类
type PartialTextTs = Partial<TextTS>
let test2: TextTS = {
  //报错 ,说明属性必填
  a:  "1",
  b:  "2",
  c:  "3"
}
let test1: PartialTextTs = {
  //正常,说明属性可选
}

Required <Type>

新定义 一个类型,将所有属性变为必选的类

//1.1 Required
type Person =  Required<TextTS>
let test3: Person = {
  a : "1",
  b : "2",
  c : "3"

}

Readonly<Type>

新定义 一个类型, 将所有属性变为只读的类

//2Readonly <Type> 新定义 一个类型, 将所有属性变为只读的类
type ReadonlyTextTs = Readonly<TextTS>
let testReadonly: ReadonlyTextTs = {
  a: "1",
  b: "2",
  c: "3"
}
let test12: TextTS = {
  a: "1",
  b: "2",
  c: "3"
}
testReadonly.a = "f" //报错,说明只读
test12.a = "f"

Pick<Type,Keys>

新定义 一个类型 从Type类型中,选择需要的keys,构成新的类型
这是不支持么,居然报错
Some of utility types are not supported (arkts-no-utility-types) <ArkTSCheck>
在这里插入图片描述
如此写,就不会报错了
在这里插入图片描述

Record<Keys,Type>

新定义 一个类型属性键为Keys,属性类型为Type,构成新的类型 居然报错
Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals) <ArkTSCheck>
在这里插入图片描述
如此写是正常的,所以在此处需要注意ke的类型和使用方式哦~
在这里插入图片描述

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值