简体   繁体   English

如何在属性中使用 class 以及使用 Object.assign 时使用 jsdoc?

[英]How to use jsdoc when class in property, and when using Object.assign?

My goal我的目标

  1. to add objects of defined type when creating objects在创建对象时添加已定义类型的对象
  2. methods are hidden in a namespace方法隐藏在命名空间中

I have two questions in the code below我在下面的代码中有两个问题

  1. the type of result1 does not show the full type when using Object.assign使用 Object.assign 时 result1 的类型不显示完整类型
  2. the type of result2 always shows typeof Result, but I expect it to be the type of instance result2 的类型总是显示 typeof Result,但我希望它是实例的类型

Code explanation代码说明

Result will be created, when creating I need to add A and B_plus (possibly other extended classes) and will new an Object in other js files.结果将被创建,创建时我需要添加 A 和 B_plus(可能是其他扩展类),并将在其他 js 文件中新建一个 Object。 There is no way to use module in this project.在这个项目中没有办法使用模块。

If the above problems cannot be solved, what is a better way to achieve my goal?如果上述问题都无法解决,有什么更好的方法可以实现我的目标?

 const Namespace = {}; { class Result {} class A { a; } class B { b; } class B_Plus extends B { bplus; } class Builder { /** * @instance */ result; constructor() { this.result = new Result(); } /** * * @param {A} a */ addA(a) { this.result = Object.assign(this.result, { a }); return this; } /** * * @param {B} b */ addB(b) { this.result = Object.assign(this.result, { b }); return this; } build() { return this.result; } } Namespace.Result = Result; Namespace.A = A; Namespace.B = B; Namespace.B_Plus = B_Plus; Namespace.Builder = Builder; /** * after build, I get {Result}. * this is the way I find to define the type * @type {Result & {a: A, b: B_Plus}} */ let result1 = new Builder().addA(new A()).addB(new B_Plus()).build(); } /** * dont know how to get correct type * at least be {Result} of {Namespace.Result} * but I get {typeof Result} * @type {typeof Namespace.Result} */ let result2 = new Namespace.Builder().addA(new Namespace.A()).addB(new Namespace.B_Plus()).build(); console.log(result2);

The following code would do the trick.以下代码可以解决问题。 Use @template for the Builder and add methods.Builder使用@templateadd方法。 Then typecast some types to be compatible with /** @type {any}(...)然后对一些类型进行类型转换以兼容/** @type {any}(...)

 const Namespace = {}; { class Result {} class A { a; } class B { b; } class B_Plus extends B { bplus; } /** * @template {Result} T */ class Builder { /** * @type {T} */ result; constructor() { this.result = /** @type {any} */(new Result()); } /** * @template {A} U * @param {U} a * @returns {Builder<T & U>}} */ addA(a) { this.result = Object.assign(this.result, { a }); return /** @type {any} */(this); } /** * @template {B} U * @param {U} b * @returns {Builder<T & U>}} */ addB(b) { this.result = Object.assign(this.result, { b }); return /** @type {any} */(this); } build() { return this.result; } } Namespace.Result = Result; Namespace.A = A; Namespace.B = B; Namespace.B_Plus = B_Plus; Namespace.Builder = Builder; let result1 = new Builder().addA(new A()).addB(new B_Plus()).build(); } let result2 = new Namespace.Builder().addA(new Namespace.A()).addB(new Namespace.B_Plus()).build(); console.log(result2);

截屏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM