Skip to content

Commit

Permalink
fix: subComponents的data字段和store字段加入isReady字段和内部字段
Browse files Browse the repository at this point in the history
  • Loading branch information
missannil committed Jan 15, 2024
1 parent 948f693 commit 06a22e0
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 22 deletions.
5 changes: 3 additions & 2 deletions src/api/SubComponent/SubData/SubDataOption.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { V } from "hry-types";
import type { InnerFields } from "../../../types/InnerData";

export type SubDataOption<TSubData extends object, legalKeys extends PropertyKey, TPrefix extends string> = {
/**
Expand All @@ -9,9 +10,9 @@ export type SubDataOption<TSubData extends object, legalKeys extends PropertyKey
& TSubData
& V.IllegalFieldValidator<
TSubData,
legalKeys | `_${TPrefix}_${string}`,
legalKeys | InnerFields<TPrefix>,
0,
"",
"⚠️ 子组件无需此字段或与Inherit字段重复 ⚠️"
"子组件无需此字段或与Inherit字段重复"
>;
};
14 changes: 12 additions & 2 deletions src/api/SubComponent/SubData/test/normal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,29 @@ SubComponent<{}, CompDoc>()({
},
});

//
SubComponent<{ data: { _num: number } }, CompDoc>()({
inherit: {
aaa_str: "wxml",
aaa_num: "wxml",
aaa_obj: "wxml",
},
data: {
// 3 CompDoc去除Inherit字段后为空时,可写内部字段
// 3 可写内部字段
_aaa_str: "str",
},
});

//
SubComponent<{ data: { _num: number } }, CompDoc>()({
inherit: {
aaa_str: "wxml",
},
data: {
// 4 可写isReady 特许字段 ExtraFields
aaa_isReady: false,
},
});

SubComponent<{}, CompDoc>()({
data: {
aaa_str: "a",
Expand Down
13 changes: 11 additions & 2 deletions src/api/SubComponent/SubInherit/SubInheritOption.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
export type SubInheritOption<TInherit> = {
import type { V } from "hry-types";

export type SubInheritOption<TInherit extends object, legalKeys extends PropertyKey> = {
/**
* 继承来自根组件的数据(key)或'wxml',wxml表示数据来自`.wxml`文件(例如数据来自父元素循环wx:for的数据).
* inherit字段最终会在选项中删除,它的存在是为了ts开发时提供心智模型(类型检测),js开发好比注释功能.便于代码阅读。
* 带类型检测
*/
inherit?: TInherit;
inherit?:
& TInherit
& V.IllegalFieldValidator<
TInherit,
legalKeys,
0,
""
>;
};
12 changes: 12 additions & 0 deletions src/api/SubComponent/SubInherit/test/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,15 @@ DefineComponent({
// @ts-expect-error 2 不能将类型“string”分配给类型“_SubComponentDoc”。
subComponents: [subDoc],
});

// 3 错误的key检查

SubComponent<Mock_RootDoc, Mock_CompDoc>()({
inherit: {
aaa_num: "required_num",
// @ts-expect-error 3.1 不存在于组件中的key给错误提示
xxx: "",
// @ts-expect-error 3.2 不存在于组件中的key给错误提示
aaa_xxx: "",
},
});
7 changes: 4 additions & 3 deletions src/api/SubComponent/SubStore/SubStoreOption.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { V } from "hry-types";
import type { InnerFields } from "../../../types/InnerData";

export type SubStoreOption<TSubStore extends object, legalKeys extends PropertyKey> = {
export type SubStoreOption<TSubStore extends object, legalKeys extends PropertyKey, TPrefix extends string> = {
/**
* 全局响应式数据字段,全局store对应数据变化实例对应数据自动setData。
* 约束为组件properties字段去除inherit和data的剩余字段和内部字段
Expand All @@ -19,8 +20,8 @@ export type SubStoreOption<TSubStore extends object, legalKeys extends PropertyK
& TSubStore
& V.IllegalFieldValidator<
TSubStore,
legalKeys,
// legalKeys | `_${TPrefix}_${string}`,
// legalKeys,
legalKeys | InnerFields<TPrefix>,
0,
"",
"与inherit和data字段重复或前缀错误"
Expand Down
5 changes: 5 additions & 0 deletions src/api/SubComponent/SubStore/test/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ SubComponent<{
},
data: {
aaa_num: 20,
aaa_isReady: false,
},
store: {
// @ts-expect-error 1 与 inherit 字段重复
Expand All @@ -33,5 +34,9 @@ SubComponent<{
aaa_num: () => user.age,
// @ts-expect-error 3 超出约束字段
num: () => user.age,
// @ts-expect-error 4 于data字段重复
aaa_isReady() {
return false;
},
},
});
16 changes: 9 additions & 7 deletions src/api/SubComponent/SubStore/test/normal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ type DocA = ComponentDoc<{

SubComponent<{}, DocA>()({
store: {
// 1 可写组件字段
aaa_str: () => user.name,

aaa_num: () => user.age,
// 内部字段
// _aaa_ddd: () => user.age,
},
});

SubComponent<{}, DocA, "a">()({
store: {
aaaA_str: () => user.name,
// 2 可写内部字段
_aaaA_xxxxxx: () => user.age,
},
});

aaaA_num: () => user.age,
// 内部字段
// _aaaA_ddd: () => user.age,
SubComponent<{}, DocA, "a">()({
store: {
// 3 可写特许字段
aaaA_isReady: () => false,
},
});
24 changes: 18 additions & 6 deletions src/api/SubComponent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type { ReplacePrefix } from "../../types/ReplacePrefix";
import type { ComponentDoc } from "../DefineComponent/ReturnType/ComponentDoc";

import type { Func } from "hry-types/src/Misc/Func";
import type { Extra } from "../../types/Extra";
import type { InnerFields } from "../../types/InnerData";
import type { WMCompOtherOption } from "../../types/OfficialTypeAlias";
import type { Replace } from "../../types/Replace";
import type { IInjectStore } from "../InstanceInject/instanceConfig";
Expand Down Expand Up @@ -56,15 +58,16 @@ type Options<
SubEventsDoc extends object,
SubMethodsDoc extends object,
> =
& SubInheritOption<TInherit>
& SubInheritOption<TInherit, keyof CurrentCompDoc["properties"]>
& SubDataOption<
TSubData,
Exclude<keyof CurrentCompDoc["properties"], (keyof InheritDoc)>,
Exclude<keyof CurrentCompDoc["properties"] | keyof Extra<Prefix>, (keyof InheritDoc)>,
Prefix
>
& SubStoreOption<
TSubStore,
Exclude<keyof CurrentCompDoc["properties"], (keyof (InheritDoc & SubDataDoc))>
Exclude<keyof CurrentCompDoc["properties"] | keyof Extra<Prefix>, (keyof (InheritDoc & SubDataDoc))>,
Prefix
>
& SubComputedOption<
TSubComputed,
Expand Down Expand Up @@ -126,12 +129,21 @@ type SubComponentConstructor<
> = {
<
TInherit extends InheritConstraint<AllRootDataDoc, CurrentCompDoc>,
TSubData extends SubDataConstraint<Omit<Required<CurrentCompDoc["properties"]>, keyof InheritDoc>>,
TSubStore extends SubStoreConstraint<Omit<Required<CurrentCompDoc["properties"]>, keyof (InheritDoc & SubDataDoc)>>,
TSubData extends SubDataConstraint<
& Omit<Required<CurrentCompDoc["properties"]>, keyof InheritDoc>
& Extra<CurrentPrefix> // 加入特许字段 isReady
& Record<InnerFields<CurrentPrefix>, unknown> // 内部字段
>,
TSubStore extends SubStoreConstraint<
& Omit<Required<CurrentCompDoc["properties"]> & Extra<CurrentPrefix>, keyof (InheritDoc & SubDataDoc)>
& Record<InnerFields<CurrentPrefix>, unknown> // 内部字段
>,
TEvents extends SubEventsConstraint<CurrentCompDoc>,
// 加默认值计算字段无提示且需要手写返回类型,不加watch无法对computed监控
TSubComputed extends SubComputedConstraint<
Omit<Required<CurrentCompDoc["properties"]>, keyof (InheritDoc & SubDataDoc & SubStoreDoc)>
& Omit<Required<CurrentCompDoc["properties"]>, keyof (InheritDoc & SubDataDoc & SubStoreDoc)>
& Extra<CurrentPrefix> // 加入特许字段 isReady
& Record<InnerFields<CurrentPrefix>, unknown> // 内部字段
> = {},
TSubMethods extends SubMethodsConstraint = {},
InheritDoc extends object = IfExtends<InheritConstraint<AllRootDataDoc, CurrentCompDoc>, TInherit, {}, TInherit>,
Expand Down
1 change: 1 addition & 0 deletions src/types/Extra.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Extra<Prefix extends string> = Record<`${Prefix}_isReady`, boolean>;
1 change: 1 addition & 0 deletions src/types/InnerData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type InnerFields<Prefix extends string> = `_${Prefix}_${string}`;

0 comments on commit 06a22e0

Please sign in to comment.