简体   繁体   English

Typescript 推断类型失败

[英]Typescript inferring type fails

I'm using typescript version ^4.1.3 and have a REST API with albums and arts collections.我正在使用 typescript 版本 ^4.1.3 并有一个 REST API 与专辑和艺术 Z0B9ABFE67CCA91FCF6ZECD1。 Before I send the request response to web client, I remove the userId from collections.在我向 web 客户端发送请求响应之前,我从 collections 中删除了 userId。

Please see my Album and Arts interfaces from DynamoDB tables, and the utility method to remove the userId property from collection objects.请查看我的 DynamoDB 表中的 Album 和 Arts 接口,以及从集合对象中删除 userId 属性的实用方法。

export interface Album {
    userId: string;
    albumId: string;
    creationDate: string;
    visibility: AlbumVisibility;
    title: string;
    description: string;
    coverUrl: string;
}

export interface Art {
    albumId: string;
    artId: string;
    sequenceNum: number;
    userId: string;
    creationDate: string;
    title: string;
    description: string;
    imgUrl: string;
}

export function rmUserIdFromArr(items: Album[] | Art[]) {
    return items.map((item: Album | Art) => {
        const { userId, ...newItem } = item;
        return newItem;
    });
}

But when I run the code in a collection of arts items, Typescript is complaining of:但是当我在一系列艺术项目中运行代码时,Typescript 抱怨:

const arts = rmUserIdFromArr(result.items).map((item) => ({
        ...item,
        imgUrl: getDownloadSignedUrl(getFsArtsFolder(item.albumId), item.artId),
    }));

Property ' artId ' does not exist on type '{ albumId: string;类型 '{ albumId: string; 上不存在属性 ' artId ' creationDate: string;创建日期:字符串; visibility: >AlbumVisibility;可见性:>相册可见性; title: string;标题:字符串; description: string;描述:字符串; coverUrl: string;封面网址:字符串; } | } | { albumId: string; { 专辑 ID:字符串; > artId : string; >艺术标识:字符串; sequenceNum: number;序列号:数字; creationDate: string;创建日期:字符串; title: string;标题:字符串; description: string;描述:字符串; >imgUrl: string; >imgUrl:字符串; }'. }'。 Property 'artId' does not exist on type '{ albumId: string;类型 '{ albumId: string; 上不存在属性 'artId' creationDate: string;创建日期:字符串; visibility: >AlbumVisibility;可见性:>相册可见性; title: string;标题:字符串; description: string;描述:字符串; coverUrl: string;封面网址:字符串; }'.ts(2339) }'.ts(2339)

This is strange to me, because the artId exists in the Art interface type.这对我来说很奇怪,因为 artId 存在于 Art 接口类型中。

Am I missing something here?我在这里错过了什么吗? I know I can fix the issue setting the item from map function as item: any , but I was wondering if there`sa better way to fix this.我知道我可以解决将 map function 中的项目设置为item: any的问题,但我想知道是否有更好的方法来解决这个问题。

const arts = rmUserIdFromArr(result.items).map((item: any) => ({

Thank you very much, BR.非常感谢,BR。

I believe you should rewrite your function to something more generic:我相信您应该将 function 重写为更通用的内容:

function rmUserIdFromArr<T extends { userId: string }>(items: T[]): Omit<T, 'userId'>[] {
    return items.map((item) => {
        const { userId, ...newItem } = item;
        return newItem;
    });
}

playground link游乐场链接

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

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