-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
126 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]> | ||
> | ||
>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]> | ||
> | ||
>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.