@zwa73/utils 1.0.61 → 1.0.62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -85,4 +85,19 @@ export type ComposedClass<Base extends object, Mixin extends object> = Base & Mi
85
85
  * @template MixinList- 待混入的类型数组
86
86
  */
87
87
  export type ComposedClassMult<Base extends object, MixinList extends object[]> = MixinList extends [infer Mixin, ...infer Rest] ? Mixin extends object ? Rest extends object[] ? ComposedClassMult<ComposedClass<Base, Mixin>, Rest> : Base : Base : Base;
88
+ /**将一个类型的所有方法的 `this` 参数改为 `T` 类型
89
+ * @template Methods - 待更改的函数表
90
+ * @template T - 目标this类型
91
+ */
92
+ export type MethodsThisAs<Methods, T> = {
93
+ [K in keyof Methods]: Methods[K] extends (...args: infer A) => infer R ? (this: T, ...args: A) => R : Methods[K];
94
+ };
95
+ /**尝试断言一个类的原型
96
+ * 其所有函数的 this 都必须为某个类型
97
+ * assignThisAs<Self,T>(Self.prototype);
98
+ * @template Self - 类的类型
99
+ * @template T - 目标this类型
100
+ * @param prototype - 类的原型
101
+ */
102
+ export declare function assignThisAs<Self, T>(prototype: MethodsThisAs<Self, T>): void;
88
103
  export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.stringifyJToken = void 0;
3
+ exports.assignThisAs = exports.stringifyJToken = void 0;
4
4
  /**将JToken转换为字符串
5
5
  * @param token - 待转换的Token
6
6
  * @param space - 插入的空格 数字为空格数量 默认为制表符\t
@@ -12,3 +12,12 @@ function stringifyJToken(token, space = "\t") {
12
12
  return JSON.stringify(token, null, space);
13
13
  }
14
14
  exports.stringifyJToken = stringifyJToken;
15
+ /**尝试断言一个类的原型
16
+ * 其所有函数的 this 都必须为某个类型
17
+ * assignThisAs<Self,T>(Self.prototype);
18
+ * @template Self - 类的类型
19
+ * @template T - 目标this类型
20
+ * @param prototype - 类的原型
21
+ */
22
+ function assignThisAs(prototype) { }
23
+ exports.assignThisAs = assignThisAs;
@@ -14,8 +14,13 @@ declare class B {
14
14
  /**保留的函数 */
15
15
  privateFunc(): void;
16
16
  }
17
- export declare const C: import("..").ComposedClassPart<{
18
- create(): A & Pick<B, "getB" | "getText"> & Record<"bindFunc", () => "这是动态绑定的函数">;
19
- }, typeof B, "getNewB">;
20
- export type C = ReturnType<typeof C.create>;
17
+ declare class _C {
18
+ static create(): _C & Pick<B, "getB" | "getText"> & Pick<A, "getA" | "getNum"> & Record<"bindFunc", () => "这是动态绑定的函数">;
19
+ private constructor();
20
+ getC(this: C): _C & Pick<B, "getB" | "getText"> & Pick<A, "getA" | "getNum"> & Record<"bindFunc", () => "这是动态绑定的函数">;
21
+ testFunc(this: C): number;
22
+ }
23
+ declare function composeC(obj: _C): _C & Pick<B, "getB" | "getText"> & Pick<A, "getA" | "getNum"> & Record<"bindFunc", () => "这是动态绑定的函数">;
24
+ export type C = ReturnType<typeof composeC>;
25
+ export declare const C: typeof _C;
21
26
  export {};
@@ -16,18 +16,35 @@ class B {
16
16
  /**B的静态函数 */
17
17
  static getNewB = () => new B();
18
18
  /**保留的函数 */
19
- privateFunc() { }
19
+ privateFunc() {
20
+ console.log();
21
+ }
20
22
  }
21
- exports.C = __1.UtilFunc.composeClassPart({
22
- create() {
23
- let o1 = __1.UtilFunc.composeClassPart(new A(), new B(), 'getB', 'getText');
24
- let o2 = __1.UtilFP.bindTo('bindFunc', () => "这是动态绑定的函数", o1);
25
- return o2;
23
+ class _C {
24
+ static create() {
25
+ return new _C();
26
+ }
27
+ constructor() {
28
+ composeC(this);
29
+ }
30
+ getC() { return this; }
31
+ ;
32
+ testFunc() {
33
+ console.log(this);
34
+ return 2;
26
35
  }
27
- }, B, 'getNewB');
36
+ }
37
+ function composeC(obj) {
38
+ let ob1 = __1.UtilFunc.composeClassPart(obj, new B(), 'getB', 'getText');
39
+ let ob2 = __1.UtilFunc.composeClassPart(ob1, new A(), 'getA', 'getNum');
40
+ let ob3 = __1.UtilFP.bindTo('bindFunc', () => "这是动态绑定的函数", ob2);
41
+ return ob3;
42
+ }
43
+ exports.C = _C;
44
+ (0, __1.assignThisAs)(exports.C.prototype);
28
45
  const insc = exports.C.create(); //?
29
- exports.C.getNewB().getB().getText(); //?
46
+ insc.getC().testFunc(); //?
47
+ insc.testFunc(); //?
30
48
  insc.getText(); //?
31
49
  insc.getB().getText(); //?
32
- insc.getNum(); //?
33
50
  insc.bindFunc(); //?
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.61",
3
+ "version": "1.0.62",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -135,4 +135,24 @@ export type ComposedClassMult<Base extends object,MixinList extends object[]> =
135
135
  ? ComposedClassMult<ComposedClass<Base,Mixin>,Rest>
136
136
  : Base
137
137
  : Base
138
- : Base;
138
+ : Base;
139
+
140
+ /**将一个类型的所有方法的 `this` 参数改为 `T` 类型
141
+ * @template Methods - 待更改的函数表
142
+ * @template T - 目标this类型
143
+ */
144
+ export type MethodsThisAs<Methods, T> = {
145
+ [K in keyof Methods]:
146
+ Methods[K] extends (...args: infer A) => infer R
147
+ ? (this: T, ...args: A) => R
148
+ : Methods[K];
149
+ };
150
+
151
+ /**尝试断言一个类的原型
152
+ * 其所有函数的 this 都必须为某个类型
153
+ * assignThisAs<Self,T>(Self.prototype);
154
+ * @template Self - 类的类型
155
+ * @template T - 目标this类型
156
+ * @param prototype - 类的原型
157
+ */
158
+ export function assignThisAs<Self,T>(prototype: MethodsThisAs<Self, T>) {}
@@ -1,8 +1,4 @@
1
- import { UtilFP, UtilFunc } from ".."
2
-
3
-
4
-
5
-
1
+ import { UtilFP, UtilFunc, assignThisAs } from ".."
6
2
 
7
3
  class A {
8
4
  private num = 1;
@@ -18,20 +14,37 @@ class B {
18
14
  /**B的静态函数 */
19
15
  static getNewB = ()=>new B();
20
16
  /**保留的函数 */
21
- privateFunc(){}
17
+ privateFunc(){
18
+ console.log()
19
+ }
22
20
  }
23
- export const C = UtilFunc.composeClassPart({
24
- create(){
25
- let o1 = UtilFunc.composeClassPart(new A(),new B(),'getB','getText');
26
- let o2 = UtilFP.bindTo('bindFunc',()=>"这是动态绑定的函数",o1);
27
- return o2;
21
+ class _C {
22
+ static create(){
23
+ return new _C() as C;
28
24
  }
29
- },B,'getNewB');
30
- export type C = ReturnType<typeof C.create>;
25
+ private constructor() {
26
+ composeC(this)
27
+ }
28
+ getC(this:C){return this};
29
+ testFunc(this:C){
30
+ console.log(this);
31
+ return 2;
32
+ }
33
+ }
34
+ function composeC(obj:_C){
35
+ let ob1 = UtilFunc.composeClassPart(obj,new B(),'getB','getText');
36
+ let ob2 = UtilFunc.composeClassPart(ob1,new A(),'getA','getNum');
37
+ let ob3 = UtilFP.bindTo('bindFunc',()=>"这是动态绑定的函数",ob2);
38
+ return ob3;
39
+ }
40
+ export type C = ReturnType<typeof composeC>;
41
+ export const C = _C;
42
+ assignThisAs<_C,C>(C.prototype);
43
+
31
44
  const insc:C=C.create();//?
32
- C.getNewB().getB().getText()//?
45
+ insc.getC().testFunc()//?
46
+ insc.testFunc();//?
33
47
  insc.getText();//?
34
48
  insc.getB().getText()//?
35
- insc.getNum()//?
36
49
  insc.bindFunc()//?
37
50