多个索引器不允许出现错误,但它们不是索引器 [英] Multiple indexers not allowed error, but they are not indexers

查看:64
本文介绍了多个索引器不允许出现错误,但它们不是索引器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是创建一个Entities类型,它是K中所有键的精确对象类型".

My goal is to create an Entities type, which is an "exact object type" of all the keys in K.

K是:

type SocialEntity =     Article    | Thumb    | Comment    | Displayname    | Helpful;
type SocialEntityKind = 'articles' | 'thumbs' | 'comments' | 'displaynames' | 'helpfuls';


const K:  {[key: SocialEntityKind]: SocialEntityKind} = { // short for SOCIAL_ENTITY_KIND
    articles: 'articles',
    thumbs: 'thumbs',
    comments: 'comments',
    displaynames: 'displaynames',
    helpfuls: 'helpfuls'
}

现在,我尝试设置[key: typeof K.*],但考虑了这些索引器.

Now I tried setting [key: typeof K.*] but its considering these indexers.

type Entities = {|
    [key: typeof K.articles]: {
        [key: ArticleId]: Article
    },
    [key: typeof K.thumbs]: {
        [key: ThumbId]: Thumb
    },
    [key: typeof K.comments]: {
        [key: CommentId]: Comment
    },
    [key: typeof K.displaynames]: {
        [key: DisplaynameId]: Displayname
    },
    [key: typeof K.helpfuls]: {
        [key: HelpfulId]: Helpful
    }
|}

以下是错误的屏幕截图:

Here is a screenshot of the error:

有没有简单而正确的方法来做到这一点?

Is there any easy and right way to do this?

推荐答案

该语法不适用于您要尝试执行的操作. Flowtype中的[key: TYPE]: TYPE使用对象作为字典的语法,而不是计算出的属性名称的语法.

That syntax will not work for what you are trying to do. [key: TYPE]: TYPE in Flowtype is syntax for using objects as dictionaries, not syntax for computed property names.

也不可能以您想要的方式使用typeof,因为类型只是string,而不是articles.您必须手动输入键的名称.

It is also not possible to use typeof in the way you want, because the type is just string, not articles exactly. You'll have to manually put in the names of the keys.

这篇关于多个索引器不允许出现错误,但它们不是索引器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆