当前位置:   article > 正文

TypeScript高级类型 在鸿蒙中的使用 Partial、Required、Readonly、Pick、Record_鸿蒙 不支持 type partial

鸿蒙 不支持 type partial

我的工程代码在这里,持续更新中
欢迎交流,谢谢
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 = {
  //正常,说明属性可选
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

Required <Type>

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

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

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

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的类型和使用方式哦~
在这里插入图片描述

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号