Skip to content

Commit

Permalink
fix: 无注入数据时 watch监控类型错误的问题和SubComponentdata数据为字面量类型造成实例类型错误的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
missannil committed Jan 6, 2024
1 parent c7db472 commit 3cb2b90
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/api/InstanceInject/instanceConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export interface IInjectInfo extends BaseInjectInfo {

export type IInjectData = IInjectInfo["data"] & ReturnTypeInObject<IInjectInfo["store"]>;

export type IInjectStore = ReturnTypeInObject<IInjectInfo["store"]>;
export type IInjectStore = StoreConstraint extends IInjectInfo["store"] ? unknown
: ReturnTypeInObject<IInjectInfo["store"]>;

export type IInjectMethods = IInjectInfo["methods"];

Expand Down
10 changes: 5 additions & 5 deletions src/api/SubComponent/SubComputed/test/normal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ SubComponent<Root, OnlyPropsCompDoc>()({
SubComponent<Root, OnlyPropsCompDoc>()({
computed: {
aaa_num() {
return 123 as number;
return 123;
},
aaa_num123(): 123 {
return 123;
Expand All @@ -90,8 +90,8 @@ SubComponent<Root, OnlyPropsCompDoc>()({
bool: boolean;
aaa_num: number;
aaa_num123: 123;
aaa_str: "a";
aaa_obj: Mock_User;
aaa_str: "a" | "b";
aaa_obj: Mock_User | null;
} & IInjectData
>,
Test.Pass
Expand All @@ -114,8 +114,8 @@ SubComponent<Root, OnlyPropsCompDoc>()({
bool: boolean;
aaa_num: number;
aaa_num123: 123;
aaa_str: "a";
aaa_obj: Mock_User;
aaa_str: "a" | "b";
aaa_obj: Mock_User | null;
} & IInjectData
>,
Test.Pass
Expand Down
6 changes: 3 additions & 3 deletions src/api/SubComponent/SubData/test/normal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ SubComponent<{}, CompDoc>()({
typeof this.data,
ReadonlyDeep<
{
aaa_str: "a";
aaa_num: 123;
aaa_obj: null;
_aaa_str: string;
aaa_str: "a" | "b";
aaa_num: number;
aaa_obj: Mock_User | null;
} & IInjectData
>,
Test.Pass
Expand Down
4 changes: 2 additions & 2 deletions src/api/SubComponent/SubInstance/test/normal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ SubComponent<RootDoc, CompDoc>()({
PoptionalObj: Mock_User;
Dnum: number;
Cnum: number;
// 自身Data
aaa_str: "str";
// 自身Data类型与CompDoc类型相同
aaa_str: string;
} & IInjectData
>,
Test.Pass
Expand Down
5 changes: 3 additions & 2 deletions src/api/SubComponent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { ReplacePrefix } from "../../types/ReplacePrefix";
import type { ComponentDoc } from "../DefineComponent/ReturnType/ComponentDoc";

import type { Func } from "hry-types/src/Misc/Func";
import type { AssignValues } from "../../types/AssignValues";
import type { WMCompOtherOption } from "../../types/OfficialTypeAlias";
import type { IInjectStore } from "../InstanceInject/instanceConfig";
import type { ComputedConstraint } from "../RootComponent/Computed/ComputedConstraint";
Expand Down Expand Up @@ -94,8 +95,8 @@ type Options<
& ThisType<
SubInstance<
SubMethodsDoc & RootDoc["methods"],
TSubData,
AllRootDataDoc & SubDataDoc & SubComputedDoc & SubStoreDoc,
SubDataDoc,
AllRootDataDoc & AssignValues<SubDataDoc & SubComputedDoc & SubStoreDoc, Required<CurrentCompDoc["properties"]>>,
RootDoc["customEvents"] & {},
SubStoreDoc
>
Expand Down
26 changes: 26 additions & 0 deletions src/types/AssignValues.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { IfExtends } from "hry-types/src/Any/IfExtends";
import type { ComputeIntersection } from "hry-types/src/Object/ComputeIntersection";

type _AssignValues<Target, Source, UniqueKey extends keyof Target> = ComputeIntersection<
& { [k in UniqueKey]: Target[k] }
// @ts-ignore
& { [k in Exclude<keyof Target, UniqueKey>]: Source[k] }
>;

/**
* Target与Source相同的key类型变为Source的类型
* @example
* ```ts
* type obj = { a: "a"; b: "b"; c: boolean };
* type obj2 = { a: number; b: string; d: number };
* type test = AssignValues<obj, obj2>;
* // test = { a: number; b: string; c: boolean}
* type test1 = AssignValues<unknown, obj2>; // unknown
* ```
*/
export type AssignValues<Target, Source> = IfExtends<
unknown,
Target,
unknown,
_AssignValues<Target, Source, Exclude<keyof Target, keyof Source>>
>;

0 comments on commit 3cb2b90

Please sign in to comment.