Skip to content

Commit

Permalink
feat: subComponent的inherit字段加入数组类型,表示联合类型
Browse files Browse the repository at this point in the history
  • Loading branch information
missannil committed Apr 11, 2024
1 parent 95afce2 commit a7930ff
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/api/SubComponent/SubInherit/SubInheritConstraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import type { ComponentDoc } from "../../DefineComponent/ReturnType/ComponentDoc

type WXMLSign = "wxml";

type unionAddList<Keys extends string> = Keys | Keys[];

/**
* 子组件inherit字段约束,key为构建组件所需的properties字段,类型为根组件数据的key或`WXMLSign`。要求根数据类型为子数据类型的子类型。当key的值来自wxml(循环产生的子数据等情况)时用`WXMLSign`表示。
* 子组件inherit字段约束,key为构建组件所需的properties字段,类型为根组件数据的key或key[]或`WXMLSign`。要求根数据类型为子数据类型的子类型。当key的值来自wxml(循环产生的子数据等情况)时用`WXMLSign`表示。
* @returns object
*/
export type InheritConstraint<AllRootData extends object, TComponentDoc extends ComponentDoc> = {
[k in keyof TComponentDoc["properties"]]?:
| RemoveInnerData<SelectKeys<AllRootData, TComponentDoc["properties"][k]> & string>
| unionAddList<RemoveInnerData<SelectKeys<AllRootData, TComponentDoc["properties"][k]> & string>>
| WXMLSign;
};
8 changes: 8 additions & 0 deletions src/api/SubComponent/SubInherit/test/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,11 @@ SubComponent<Mock_RootDoc, Mock_CompDoc>()({
aaa_xxx: "",
},
});
// 4 数组时类型错误

SubComponent<Mock_RootDoc, Mock_CompDoc>()({
inherit: {
// @ts-expect-error
aaa_num: ["optional_lit1eral_num", "require1d_num"],
},
});
11 changes: 11 additions & 0 deletions src/api/SubComponent/SubInherit/test/normal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,14 @@ SubComponent<Mock_RootDoc, Mock_CompDoc>()({
aaa_obj1: "" as "required_obj" | "optional_obj" | "wxml",
},
});

// 3 value为数组时,表示多个字段的联合类型

SubComponent<Mock_RootDoc, Mock_CompDoc>()({
inherit: {
aaa_num: ["optional_literal_num", "required_num"],
aaa_str: ["literal_str", "str"],
aaa_obj: "" as "required_obj" | "optional_obj" | ["required_obj" | "optional_obj"] | "wxml",
aaa_obj1: "" as "required_obj" | "optional_obj" | ["required_obj" | "optional_obj"] | "wxml",
},
});

0 comments on commit a7930ff

Please sign in to comment.