Skip to content

Commit

Permalink
fix: 组件类型中别名被解析的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
missannil committed Dec 3, 2023
1 parent f4c0f3b commit 67e4886
Show file tree
Hide file tree
Showing 18 changed files with 126 additions and 138 deletions.
4 changes: 2 additions & 2 deletions src/api/DefineComponent/ReturnType/CreateComponentDoc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IfExtends } from "hry-types/src/Any/IfExtends";
import type { ComputeIntersectionDeep } from "hry-types/src/Object/ComputeIntersectionDeep";
import type { ComputeIntersection } from "hry-types/src/Object/ComputeIntersection";
import type { AddNullForObject } from "../../../types/AddNullForObject";
import type { RootComponentDoc } from "../../RootComponent/RootComponentDoc";
import type { SubComponentDoc } from "../../SubComponent/SubComponentDoc";
Expand All @@ -19,7 +19,7 @@ export type CreateComponentDoc<
& GetCustomEventDocOfSubDoc<TSubComponentTuple[number]>,
StopKeys extends string = GetStopKeys<TRootDoc["events"]>,
FinalCustomEventDoc extends object = Omit<AllCustomEventsDoc, StopKeys>,
> = ComputeIntersectionDeep<
> = ComputeIntersection<
& IfExtends<
unknown,
AllPropertiesDoc,
Expand Down
4 changes: 2 additions & 2 deletions src/api/DefineComponent/ReturnType/CreatePageDoc.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { IfExtends } from "hry-types/src/Any/IfExtends";
import type { ComputeIntersectionDeep } from "hry-types/src/Object/ComputeIntersectionDeep";
import type { ComputeIntersection } from "hry-types/src/Object/ComputeIntersection";
import type { RootComponentDoc } from "../../RootComponent/RootComponentDoc";

export type CreatePageDoc<
TRootDoc extends RootComponentDoc,
TPath extends string,
> = ComputeIntersectionDeep<
> = ComputeIntersection<
& { path: TPath }
& IfExtends<
unknown,
Expand Down
2 changes: 0 additions & 2 deletions src/api/RootComponent/CustomEvents/GetCustomEventDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,3 @@ export type GetCustomEventDoc<T extends CustomEventConstraint> =
| AddTagForCustomEventsDoc<T[k]["options"]>;
}
;

// type ddd = IsUnion
21 changes: 10 additions & 11 deletions src/api/RootComponent/Properties/GetOptionalDoc.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
import type { IfExtends } from "hry-types/src/Any/IfExtends";
import type { Select } from "hry-types/src/Object/Select";
import type { AddNullForObject } from "../../../types/AddNullForObject";
import type { InferDetailedType } from "../../../types/InferDetailedType";
import type { OptionalType, PropertiesConstraint } from "./PropertiesConstraint";
import type { OptionalType } from "./PropertiesConstraint";

/**
* 获取properties可传字段文档类型
*/
export type GetOptionalDoc<
TProperties extends PropertiesConstraint,
TOptionalProperties extends Record<string, OptionalType>,
TIsPage extends boolean,
// @ts-ignore
Optional extends Record<string, OptionalType> = Select<TProperties, OptionalType>,
> = {
[k in keyof Optional]?: IfExtends<
-readonly [k in keyof TOptionalProperties]?: IfExtends<
unknown,
Optional[k]["optionalTypes"],
TOptionalProperties[k]["optionalTypes"],
IfExtends<
false,
TIsPage,
// 组件时 对象加null
AddNullForObject<InferDetailedType<Optional[k]["type"]>>,
AddNullForObject<InferDetailedType<TOptionalProperties[k]["type"]>>,
// 页面时 对象不加
InferDetailedType<Optional[k]["type"]>
InferDetailedType<TOptionalProperties[k]["type"]>
>,
IfExtends<
false,
TIsPage,
// 组件时 对象加null
AddNullForObject<
InferDetailedType<Optional[k]["type"]> | InferDetailedType<(Optional[k]["optionalTypes"] & {})[number]>
| InferDetailedType<TOptionalProperties[k]["type"]>
| InferDetailedType<(TOptionalProperties[k]["optionalTypes"] & {})[number]>
>,
// 页面时 对象不加
InferDetailedType<Optional[k]["type"]> | InferDetailedType<(Optional[k]["optionalTypes"] & {})[number]>
| InferDetailedType<TOptionalProperties[k]["type"]>
| InferDetailedType<(TOptionalProperties[k]["optionalTypes"] & {})[number]>
>
>;
};
22 changes: 6 additions & 16 deletions src/api/RootComponent/Properties/GetPropertiesDoc.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
import type { IfExtends } from "hry-types/src/Any/IfExtends";
import type { Select } from "hry-types/src/Object/Select";

import type { ComputeIntersection, NonReadonlyDeep } from "hry-types/src/Object/_api";
import type { ComputeIntersection } from "hry-types/src/Object/_api";
import type { GetOptionalDoc } from "./GetOptionalDoc";
import type { GetRequiredDoc } from "./GetRequiredDoc";
import type { OptionalType, PropertiesConstraint } from "./PropertiesConstraint";

type _GetPropertiesDoc<
TProperties extends PropertiesConstraint,
TIsPage extends boolean,
Type extends "Required" | "Optional" | "all" = "all",
OptionalDoc = NonReadonlyDeep<GetOptionalDoc<Select<TProperties, OptionalType>, TIsPage>>,
RequiredDoc = NonReadonlyDeep<GetRequiredDoc<Omit<TProperties, keyof OptionalDoc>, TIsPage>>,
> = IfExtends<
Type,
"all",
// @ts-ignore
ComputeIntersection<OptionalDoc & RequiredDoc>,
IfExtends<Type, "Optional", OptionalDoc, RequiredDoc>
>;
OptionalDoc extends object = GetOptionalDoc<Select<TProperties, OptionalType>, TIsPage>,
// @ts-ignore
RequiredDoc extends object = GetRequiredDoc<Omit<TProperties, keyof OptionalDoc>, TIsPage>,
> = ComputeIntersection<OptionalDoc & RequiredDoc>;

/**
* properties字段的文档类型
* @param TProperties - `PropertiesConstraint`
* @param Type - "Required" | "Optional" | "all"
* @returns `GetRequiredDoc` | `GetOptionalDoc` | (`GetOptionalDoc` & `GetRequiredDoc`)
*/
export type GetPropertiesDoc<
TProperties extends PropertiesConstraint,
TisPage extends boolean,
Type extends "Required" | "Optional" | "all" = "all",
> = _GetPropertiesDoc<TProperties, TisPage, Type>;
> = _GetPropertiesDoc<TProperties, TisPage>;
40 changes: 25 additions & 15 deletions src/api/RootComponent/Properties/GetRequiredDoc.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
import type { IfExtends } from "hry-types/src/Any/IfExtends";
import type { Select } from "hry-types/src/Object/Select";
import type { AddNullForObject } from "../../../types/AddNullForObject";
import type { InferDetailedType } from "../../../types/InferDetailedType";
import type { PropertiesConstraint, RequiredSingle, RequiredType } from "./PropertiesConstraint";
import type { RequiredSingle, RequiredType } from "./PropertiesConstraint";

/**
* 获取properties必传字段的文档类型
* @remarks 小程序中必传字段如果是object,默认类型为null,所以文档类型要联合上null类型
* @remarks 小程序中必传字段如果是object,默认类型为null,所以组件文档对象类型要联合上null类型
*/
export type GetRequiredDoc<
TProperties extends PropertiesConstraint,
TProperties extends Record<string, RequiredType>,
TIsPage extends boolean,
// @ts-ignore
Required extends Record<string, RequiredType> = Select<TProperties, RequiredType>,
> = {
[k in keyof Required]: IfExtends<
Required[k],
-readonly [k in keyof TProperties]: IfExtends<
TProperties[k],
// 非对象写法
RequiredSingle,
IfExtends<false, TIsPage, AddNullForObject<InferDetailedType<Required[k]>>, InferDetailedType<Required[k]>>,
IfExtends<
false,
TIsPage,
// @ts-ignore Required[k] 必为 RequiredUnion
| AddNullForObject<InferDetailedType<Required[k]["type"]>>
// @ts-ignore Required[k] 必为 RequiredUnion
| AddNullForObject<InferDetailedType<Required[k]["optionalTypes"][number]>>,
// @ts-ignore Required[k] 必为 RequiredUnion
InferDetailedType<Required[k]["type"]> | InferDetailedType<Required[k]["optionalTypes"][number]>
// 组件的对象类型加null
AddNullForObject<InferDetailedType<TProperties[k]>>,
// 页面的对象类型不加null
InferDetailedType<TProperties[k]>
>,
// 对象写法 RequiredUnion
IfExtends<
false,
TIsPage,
// 组件的对象类型加null
AddNullForObject<
// @ts-ignore TProperties[k] 必为 RequiredUnion
| InferDetailedType<TProperties[k]["type"]>
// @ts-ignore TProperties[k] 必为 RequiredUnion
| InferDetailedType<TProperties[k]["optionalTypes"][number]>
>,
// 页面的对象类型不加null
// @ts-ignore TProperties[k] 必为 RequiredUnion
InferDetailedType<TProperties[k]["type"]> | InferDetailedType<TProperties[k]["optionalTypes"][number]>
>
>;
};
12 changes: 11 additions & 1 deletion src/api/RootComponent/Store/test/normal.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { Checking, type Test } from "hry-types";
import { observable } from "mobx";
import { RootComponent } from "../..";
const user = observable({
name: "zhao",
age: 20,
});

RootComponent()({
const storeDoc = RootComponent()({
store: {
userName: () => user.name,
userAge: () => user.age,
},
});

type StoreDocExpected = {
store: {
userName: string;
userAge: number;
};
};

Checking<typeof storeDoc, StoreDocExpected, Test.Pass>;
11 changes: 6 additions & 5 deletions src/api/RootComponent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { IfExtends } from "hry-types/src/Any/IfExtends";
import type { NoInfer } from "hry-types/src/Generic/NoInfer";
import type { EmptyObject } from "hry-types/src/Misc/EmptyObject";
import type { Func } from "hry-types/src/Misc/Func";
import type { ComputeIntersectionDeep } from "hry-types/src/Object/_api";
import type { ComputeIntersection } from "hry-types/src/Object/_api";
import type { ComputeObject } from "../../types/ComputeObj";
import type { WMCompOtherOption, WMCompPageLifetimes, WMPageLifetimes } from "../../types/OfficialTypeAlias";
import type { ComponentDoc } from "../DefineComponent/ReturnType/ComponentDoc";
import type { ComputedConstraint } from "./Computed/ComputedConstraint";
Expand Down Expand Up @@ -111,16 +112,16 @@ type RootComponentConstructor<TReceivedComponentDoc extends ComponentDoc[] | Com
StoreDoc,
ComputedDoc
>,
): // 返回类型 RootComponentDoc
ComputeIntersectionDeep<
): // 返回类型 satisfies RootComponentDoc
ComputeIntersection<
& IfExtends<TIsPage, false, {}, { isPage: true }>
& IfExtends<EmptyObject, PropertiesDoc, {}, { properties: PropertiesDoc }>
& IfExtends<EmptyObject, DataDoc, {}, { data: DataDoc }>
& IfExtends<EmptyObject, StoreDoc, {}, { store: StoreDoc }>
& IfExtends<EmptyObject, StoreDoc, {}, { store: ComputeObject<StoreDoc> }>
& IfExtends<EmptyObject, ComputedDoc, {}, { computed: ComputedDoc }>
& IfExtends<EmptyObject, TMethods, {}, { methods: TMethods }>
& IfExtends<EmptyObject, EventsDoc, {}, { events: EventsDoc }>
& IfExtends<EmptyObject, CustomEventsDoc, {}, { customEvents: CustomEventsDoc }>
& IfExtends<EmptyObject, CustomEventsDoc, {}, { customEvents: ComputeObject<CustomEventsDoc> }>
>;
};

Expand Down
7 changes: 5 additions & 2 deletions src/api/SubComponent/SubData/test/normal.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Checking, type Test } from "hry-types";
import type { ReadonlyDeep } from "hry-types/src/Any/_api";
import type { Wm } from "../../../../thirdLib";
import type { CompDocExtends } from "../../../../types/CompDocExtends";
import type { ComponentDocExtension } from "../../../../types/ComponentDocExtension";
import type { ComponentDoc } from "../../../DefineComponent/ReturnType/ComponentDoc";

import type { Mock_User } from "../../../RootComponent/Properties/test/normalRequired.test";
Expand All @@ -28,7 +28,10 @@ SubComponent<{}, CompDoc>()({
});

// 2. 使用CompDocExtends泛型拓展组件文档,满足内部组件内部包含一些slot或基本组件数据
type viewExtends = CompDocExtends<Wm.View, { view_obj: Mock_User | null; view_str: string; view_ddd: number }>;
type viewExtends = ComponentDocExtension<
Wm.View,
{ properties: { view_obj: Mock_User | null; view_str: string; view_ddd: number } }
>;

const res = SubComponent<{}, viewExtends>()({
inherit: {
Expand Down
4 changes: 2 additions & 2 deletions src/api/SubComponent/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IfExtends } from "hry-types/src/Any/IfExtends";
import type { EmptyObject } from "hry-types/src/Misc/EmptyObject";
import type { RequiredKeys } from "hry-types/src/Object/RequiredKeys";
import type { ExtractDocPrefix } from "../../types/GetComponentPrefix";
import type { GetComponentPrefix } from "../../types/GetComponentPrefix";
import type { ReplacePrefix } from "../../types/ReplacePrefix";
import type { ComponentDoc } from "../DefineComponent/ReturnType/ComponentDoc";

Expand Down Expand Up @@ -95,7 +95,7 @@ type SubComponentConstructor<
TSupplementalPrefix extends string = "",
IsPage extends boolean = TRootDoc["isPage"] extends true ? true : false,
// 重构子组件的前缀
CurrentPrefix extends string = `${ExtractDocPrefix<TOriginalCompDoc>}${Capitalize<TSupplementalPrefix>}`,
CurrentPrefix extends string = `${GetComponentPrefix<TOriginalCompDoc>}${Capitalize<TSupplementalPrefix>}`,
// 更新原始文档的前缀为Prefix
CurrentCompDoc extends ComponentDoc = IfExtends<
TSupplementalPrefix,
Expand Down
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
export type { Vant, Wm } from "./thirdLib";

export type { Dataset, Detail, WMBaseEvent, WMCustomEvent } from "./types/OfficialTypeAlias";

import { navigateTo } from "./api/navigateTo";

import { DefineComponent } from "./api/DefineComponent";
Expand All @@ -12,10 +8,15 @@ import { SubComponent } from "./api/SubComponent";

import type { DetailedType } from "./types/DetailedType";

import type { ComponentDocExtension } from "./types/ComponentDocExtension";
import type { GenerateDoc } from "./types/GenerateDoc";
import type { ParamsEqual } from "./types/TwoParamsEqual";
export type { Vant, Wm } from "./thirdLib";

export type { Dataset, Detail, WMBaseEvent, WMCustomEvent } from "./types/OfficialTypeAlias";

export {
type ComponentDocExtension,
DefineComponent,
type DetailedType,
type GenerateDoc,
Expand Down
14 changes: 0 additions & 14 deletions src/types/CompDocExtends.ts

This file was deleted.

36 changes: 36 additions & 0 deletions src/types/ComponentDocExtension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { IfExtends } from "hry-types/src/Any/IfExtends";
import type { ComputeIntersectionDeep } from "hry-types/src/Object/ComputeIntersectionDeep";
import type { ComponentDoc } from "../api/DefineComponent/ReturnType/ComponentDoc";
import type { GetComponentPrefix } from "./GetComponentPrefix";

type Validator<
TOriginalComponentDoc extends ComponentDoc,
TExtensionDoc extends ComponentDoc,
TOriginalPrefix extends PropertyKey = GetComponentPrefix<TOriginalComponentDoc>,
TExtenstionPrefix extends PropertyKey = GetComponentPrefix<TExtensionDoc>,
DuplicateKeys extends PropertyKey =
| Extract<keyof TExtensionDoc["properties"], keyof TOriginalComponentDoc["properties"]>
| Extract<keyof TExtensionDoc["customEvents"], keyof TOriginalComponentDoc["customEvents"]>,
> = IfExtends<
TOriginalPrefix,
TExtenstionPrefix,
IfExtends<DuplicateKeys, never, ComponentDoc, `${DuplicateKeys & string}字段重复`>,
`前缀错误,应为${TOriginalPrefix & string}`
>;

/**
* 有时组件文档内部包含一些slot或基本组件,可通过CompDocExtends泛型拓展组件文档,使得类型更安全
*/
export type ComponentDocExtension<
TOriginalComponentDoc extends ComponentDoc,
// @ts-ignore
TExtensionDoc extends Validator<TOriginalComponentDoc, TExtensionDoc>,
> = ComputeIntersectionDeep<TOriginalComponentDoc & TExtensionDoc>;

// type test0 = ComponentDocExtension<{ properties: { aaa_xxx: string } }, { properties: { aaa_xxx: string } }>; // => "aaa_xxx字段重复"

// type test1 = ComponentDocExtension<{ customEvents: { aaa_xxx: string } }, { customEvents: { aaa_xxx: string } }>; // => "aaa_xxx字段重复"

// type test2 = ComponentDocExtension<{ properties: { aaa_xxx: string } }, { properties: { ddd_xxx: string } }>; // => "前缀错误,应为aaa"

// type test3 = ComponentDocExtension<{ customEvents: { aaa_xxx: string } }, { customEvents: { ddd_xxx: string } }>; // => "前缀错误,应为aaa"
1 change: 1 addition & 0 deletions src/types/ComputeObj.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type ComputeObject<T> = T extends unknown ? { [k in keyof T]: T[k] } : never;
18 changes: 0 additions & 18 deletions src/types/Drop.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/types/DropStr.ts

This file was deleted.

0 comments on commit 67e4886

Please sign in to comment.