英文:
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<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)
答案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 'reference' in type 'Two' is not assignable to the same property in base type 'One'.
Type 'Two' is not assignable to type 'One'.
Property 'reference' is private in type 'Two' but not in type 'One'.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论