子类不能分配给具有转换器的 Firebase DocumentReference 中的超类

huangapple go评论65阅读模式
英文:

Subclass Cannot be Assigned to Superclass in Firebase DocumentReference with Converter

问题

以下是翻译好的部分:

"I'm trying to have object classes store a reference to their corresponding Firestore DocumentReference using a FirestoreDataConverter. When I declare the reference variable as public, I have no issues. But when I attempt to make it private, the TypeScript linter tells me that the subclass (Two) cannot be assigned to the type of the superclass (One)."

"Here's an example..."

abstract class One {
    protected reference?: DocumentReference<One>;

    public static converter: FirestoreDataConverter<One>;
}

class Two extends One {
    private reference?: DocumentReference<Two>;

    constructor(reference?: DocumentReference) {
        super();
        this.reference = reference?.withConverter(Two.converter);
    }
}

"Here's the full error:"

Property 'reference' in type 'Two' is not assignable to the same property in base type 'One'.
  Type 'DocumentReference<Two> | undefined' is not assignable to type 'DocumentReference<One> | undefined'.
    Type 'DocumentReference<Two>' is not assignable to type 'DocumentReference<One>'.
      Types of property 'converter' are incompatible.
        Type 'FirestoreDataConverter<Two> | null' is not assignable to type 'FirestoreDataConverter<One> | null'.
          Type 'FirestoreDataConverter<Two>' is not assignable to type 'FirestoreDataConverter<One>'.
            The types returned by 'fromFirestore(...)' are incompatible between these types.
              Type 'Two' is not assignable to type 'One'.ts(2416)
英文:

I'm trying to have object classes store a reference to their corresponding Firestore DocumentReference using a FirestoreDataConverter. When I declare the reference variable as public, I have no issues. But when I attempt to make it private, the TypeScript linter tells me that the subclass (Two) cannot be assigned to the type of the superclass (One).

Here's an example...

abstract class One {
    protected reference?: DocumentReference&lt;One&gt;;

    public static converter: FirestoreDataConverter&lt;One&gt;;
}

class Two extends One {
    private reference?: DocumentReference&lt;Two&gt;;

    constructor(reference?: DocumentReference) {
        super();
        this.reference = reference?.withConverter(Two.converter);
    }
}

Here's the full error:

Property &#39;reference&#39; in type &#39;Two&#39; is not assignable to the same property in base type &#39;One&#39;.
  Type &#39;DocumentReference&lt;Two&gt; | undefined&#39; is not assignable to type &#39;DocumentReference&lt;One&gt; | undefined&#39;.
    Type &#39;DocumentReference&lt;Two&gt;&#39; is not assignable to type &#39;DocumentReference&lt;One&gt;&#39;.
      Types of property &#39;converter&#39; are incompatible.
        Type &#39;FirestoreDataConverter&lt;Two&gt; | null&#39; is not assignable to type &#39;FirestoreDataConverter&lt;One&gt; | null&#39;.
          Type &#39;FirestoreDataConverter&lt;Two&gt;&#39; is not assignable to type &#39;FirestoreDataConverter&lt;One&gt;&#39;.
            The types returned by &#39;fromFirestore(...)&#39; are incompatible between these types.
              Type &#39;Two&#39; is not assignable to type &#39;One&#39;.ts(2416)

答案1

得分: 0

在尝试了一些其他选项后,我意识到问题在于我试图将reference属性从protected更改为private。尽管通常可以通过重新声明属性来完成此操作,但尝试将其类型分配为父类的类型会导致错误,因为它无法从protected转换为private实际错误(这是TypeScript的问题,而不是JavaScript的问题,这就解释了为什么它只作为linter中的警告提出)在这里更加明显:

class One {
    protected reference: TestOne;
}

class Two extends TestOne {
    private reference: TestTwo;
}
类型'Two'中的属性'reference'无法分配给基类型'One'中的相同属性。
  类型'Two'无法分配给类型'One'。
    类型'Two'中的属性'reference'是'private',但在类型'One'中不是。
英文:

After playing with some more options, I realized that the issue was that I was trying to convert the reference property from protected to private. While you can do this normally by re-declaring the property, trying to assign the type to that of the parent class causes an error because it cannot be converted from protected to private. The actual error (which is a TypeScript problem, not a Javascript one, explaining why this was only raised as a warning in the linter) is more visible here:

class One {
    protected reference: TestOne;
}

class Two extends TestOne {
    private reference: TestTwo;
}
Property &#39;reference&#39; in type &#39;Two&#39; is not assignable to the same property in base type &#39;One&#39;.
  Type &#39;Two&#39; is not assignable to type &#39;One&#39;.
    Property &#39;reference&#39; is private in type &#39;Two&#39; but not in type &#39;One&#39;.

huangapple
  • 本文由 发表于 2023年6月25日 22:42:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定