英文:
"Type could be instantiated with a different subtype" error using spread and rest
问题
以下是翻译好的代码部分:
type DocumentReference<T extends {} = {}> = T & {
id: number;
};
type MappableDocumentReference<T extends {} = {}> = T & {
id: string;
};
export const mapFromDocumentReference = <T extends {}>({
id,
...rest
}: DocumentReference<T>): MappableDocumentReference<T> => ({
...rest,
id: `${id}`,
});
Type 'Omit<DocumentReference<T>, "id"> & { id: string; }' is not assignable to type 'MappableDocumentReference<T>'.
Type 'Omit<DocumentReference<T>, "id"> & { id: string; }' is not assignable to type 'T'.
'Omit<DocumentReference<T>, "id">' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.(2322)
希望这对你有所帮助。如果你需要进一步的信息或有其他问题,请随时提出。
英文:
I'm trying to map an object such that the object stays the same except one property is mapped to a different type, but I'm having an issue with using spread and rest with generic types. Code and error below and playground here.
type DocumentReference<T extends {} = {}> = T & {
id: number;
};
type MappableDocumentReference<T extends {} = {}> = T & {
id: string;
};
export const mapFromDocumentReference = <T extends {}>({
id,
...rest
}: DocumentReference<T>): MappableDocumentReference<T> => ({
...rest,
id: `${id}`,
});
Type 'Omit<DocumentReference<T>, "id"> & { id: string; }' is not assignable to type 'MappableDocumentReference<T>'.
Type 'Omit<DocumentReference<T>, "id"> & { id: string; }' is not assignable to type 'T'.
'Omit<DocumentReference<T>, "id"> & { id: string; }' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.(2322)
I understand that the error is saying T
can be defined as a narrower type than {}
and the value being returned may not conform to that narrower type. But when using spread and rest I assumed that would cover narrower types since the properties of the argument would be spread into the returned value. In theory this is ensuring T
is conformed to. So I don't understand why I still get the error.
I can make this work by removing the annotation for the return type and letting TS infer the return type which is good enough for my use case. But I'd like to know why this doesn't work with the annotation.
答案1
得分: 2
问题在于 TypeScript 无法保证映射的对象仍然符合原始类型 T
。这是因为扩展操作符(...rest)不会保留类型信息,也就是 TypeScript 无法保证从 rest
中扩展的属性仍然满足 T
的约束条件。
在您的情况下,您试图返回一个包含 T
的 MappableDocumentReference<T>
。然而,TypeScript 无法保证 { ...rest, id: ${id} }
对象仍然是 T
,因为扩展操作符可能不包括 T
的所有属性(或可能包括不在 T
中的附加属性)。
修复这个问题的一种方法是将返回的对象强制转换为 MappableDocumentReference<T>
,但这不是类型安全的,如果可能的话应该避免使用:
export const mapFromDocumentReference = <T extends {}>({
id,
...rest
}: DocumentReference<T>): MappableDocumentReference<T> => ({
...rest,
id: `${id}`,
} as MappableDocumentReference<T>);
更好的解决方案是重新设计您的类型,使得 T
只包含在从 DocumentReference<T>
到 MappableDocumentReference<T>
映射时要保留的属性。这样,TypeScript 可以保证映射的对象仍然符合 T
。T
只包含在映射时要保留的属性,而 id
是与 T
分开的。这样可以确保 TypeScript 仍然能够保证映射的对象符合 T
。
type DocumentReference<T extends {} = {}> = {
data: T;
id: number;
};
type MappableDocumentReference<T extends {} = {}> = {
data: T;
id: string;
};
export const mapFromDocumentReference = <T extends {}>({
id,
data
}: DocumentReference<T>): MappableDocumentReference<T> => ({
data,
id: `${id}`,
});
英文:
The issue here is that TypeScript is unable to guarantee that the mapped object will still conform to the original type T
. This is because the spread operator (...rest) is not type-preserving, meaning that TypeScript cannot guarantee that the properties spread from rest will still satisfy the constraints of T
.
In your case, you're trying to return a MappableDocumentReference<T>
, which includes T
. However, TypeScript can't guarantee that the { ...rest, id: ${id} }
object will still be a T
, because the spread operator might not include all properties of T
(or might include additional properties that are not in T
).
One way to fix this is to cast the returned object to MappableDocumentReference<T>
, but this is not type-safe and should be avoided if possible:
export const mapFromDocumentReference = <T extends {}>({
id,
...rest
}: DocumentReference<T>): MappableDocumentReference<T> => ({
...rest,
id: `${id}`,
} as MappableDocumentReference<T>);
A better solution would be to refactor your types so that T
only includes the properties that you want to preserve when mapping from DocumentReference<T>
to MappableDocumentReference<T>
. This way, TypeScript can guarantee that the mapped object will still conform to T
. T
only includes the properties that you want to preserve when mapping, and id is separate from T
. This allows TypeScript to guarantee that the mapped object will still conform to T
.
type DocumentReference<T extends {} = {}> = {
data: T;
id: number;
};
type MappableDocumentReference<T extends {} = {}> = {
data: T;
id: string;
};
export const mapFromDocumentReference = <T extends {}>({
id,
data
}: DocumentReference<T>): MappableDocumentReference<T> => ({
data,
id: `${id}`,
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论