函数

typedef

定义一个新的类型描述

Signature:

typedef<
  T
>(
  desc: Desc<T>,
  tdesc?: _typedef_<T>
): _typedef_<T>
1
2
3
4
5
6

Parameters:

ParameterTypeDescription
descT描述类型的规则和结构
tdesc_typedef_<T>适用于先声明空的 Struct,然后再定义全部字段

Returns: _typedef_<T>

Example 1

const Person = typedef({
  name: string,
  sex: bool,
})
1
2
3
4

Example 2

// 类型别名
type Person = TypeDesc<
  Struct<{
    name: TypeDesc<string>
    sex: TypeDesc<bool>
    cosplay: Person
  }>
>
// 先声明空的 Struct
const Person: Person = typedef({})
typedef(
  {
    name: string,
    sex: bool,
    cosplay: Person, // 自引用
  },
  Person
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

typeinit

从类型描述生成默认值

Signature:

typeinit<
  T extends TypeDesc<unknown>
>(
  tdesc: T,
  literal?: literal<typeinit<T>>
): typeinit<T>
1
2
3
4
5
6

Parameters:

ParameterTypeDescription
tdescT类型描述
literalliteral<typeinit<T>>指定明确的字面值

Returns: typeinit<T>

Example

const Person = typedef({
  name: string,
  sex: bool,
})

const Ron = typeinit(Person)
// result: { name: '', sex: false }
Ron.name = 'Ron'
// result: { name: 'Ron', sex: false }

const Hermione = typeinit(Person, {
  name: 'Hermione',
  sex: true,
})
// result: { name: 'Hermione', sex: true }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

structbody

返回结构体的所有字段

Signature:

structbody<
  T extends TypeDesc<Struct<StructTypeDesc>>
>(
  tdesc: T
): _structbody_<T>
1
2
3
4
5

Parameters:

ParameterTypeDescription
tdescT结构体的类型描述

Returns: _structbody_<T>

structof

返回结构体实例的类型描述

Signature:

structof<
  T extends Struct<StructType>
>(
  struct: T
): structof<T>
1
2
3
4
5

Parameters:

ParameterTypeDescription
structT结构体实例

Returns: structof<T>

ruledef

定义规则

Signature:

ruledef<
  T extends TypeDesc<Struct<StructTypeDesc>>
>(
  tdesc: T,
  name: unknown,
  observe: observe<T>,
  executor: (self: typeinit<T>) => unknown
): void
1
2
3
4
5
6
7
8

Parameters:

ParameterTypeDescription
tdescT类型描述
nameunknown规则名
observeobserve<T>描述需要观察的字段规则
executor(self: typeinit<T>) => unknown当待观察的字段符合描述的规则时执行该规则

Returns: void

Example

const Person = typedef({
  name: string,
  sex: bool,
  intro: string,
})

// 期望当名字、性别发生变化时,自动生成个人介绍
ruledef(
  Person,
  'generateIntroduction',
  {
    name: true,
    sex: true,
  },
  (self: typeinit<typeof Person>) => {
    self.intro = `My name is ${self.name}, I am a ${self.sex ? 'girl' : 'boy'}.`
  }
)

const myself = typeinit(Person)
myself.name = 'Amy'
myself.sex = true
// 此时,预期的名字、性别发生变化了,
// 将会自动执行规则,生成个人介绍。
console.log(myself.intro)
// output: My name is Amy, I am a girl.
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

outcome

获取预期规则执行的结果

Signature:

outcome(
  struct: Struct<StructType>,
  name?: unknown
): Promise<unknown>
1
2
3
4

Parameters:

ParameterTypeDescription
structStruct<StructType>结构体实例
nameunknown规则名,如果未指定则默认获取第一个规则

Returns: Promise<unknown>

Example

// 期望当名字、性别发生变化时,自动生成个人介绍
ruledef(
  Person,
  'generateIntroduction',
  {
    name: true,
    sex: true,
  },
  (self: typeinit<typeof Person>) => {
    return (self.intro = `My name is ${self.name}, I am a ${
      self.sex ? 'girl' : 'boy'
    }.`)
  }
)

const myself = typeinit(Person)
// 先预期需要获取的结果
const asyncResult = outcome(myself, 'generateIntroduction')
myself.name = 'Amy'
myself.sex = true
// 此时,预期的名字、性别发生变化了,
// 将会自动执行规则,生成个人介绍。
console.log(await asyncResult)
// output: My name is Amy, I am a girl.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

change

手动管理数据内部变动情况(适用于 Array Set Map 等)

Signature:

change(
  obj: Record<any, any>
): void
1
2
3

Parameters:

ParameterTypeDescription
objRecord<any, any>包含内部数据的任意对象

Returns: void

wrapval

使用描述来包装值

Signature:

wrapval<
  T
>(
  desc: Record<string, unknown>,
  val?: T
): Readonly<T>
1
2
3
4
5
6

Parameters:

ParameterTypeDescription
descRecord<string, unknown>描述说明
valT被包装的值

Returns: Readonly<T>

基本类型

unknown

bool

int32

float64

string

Struct

结构体就是由一个或多个字段组成的集合。
这是一个抽象的类型,需要定义新的结构体才行。

内置类型

object

array

CArray

使用 changeProxy 实现的自动同步元素变更的数组。