流程类型奇怪的问题

问题描述 投票:0回答:1

在我的React应用程序中,我试图使所有内容都能与Flow的类型检查一起正常工作。我遇到了一个我不知道的有趣情况。这是相关的代码:

// @flow
export const UPDATE_SESSION_PROP: string = 'UPDATE_SESSION_PROP';

// This fails
type UserEmailAction = {type: UPDATE_SESSION_PROP,
                        propName: 'currentUserEmail',
                        payload: string};

// But this works
type UserEmailAction = {type: 'UPDATE_SESSION_PROP',
                        propName: 'currentUserEmail',
                        payload: string};

来自流的错误是:Cannot use string as a type because string is a value. To get the type of a value use 'typeof'.Flow(InferError)

两个type语句是否相等?

javascript reactjs flowtype
1个回答
0
投票

就像错误指出的那样,您不能使用值作为类型。您可以做的就是这样。

export type UPDATE_SESSION_PROP = 'UPDATE_SESSION_PROP';

// This works
type UserEmailAction = {type: UPDATE_SESSION_PROP,
                        propName: 'currentUserEmail',
                        payload: string};

并且第二个版本的实现实际上具有不同的含义。如果表示您将'UPDATE_SESSION_PROP'作为type

的唯一可接受值
//This works becuase `UPDATE_SESSION_PROP` is considered Literal Type
type UserEmailAction = {type: 'UPDATE_SESSION_PROP',
                        propName: 'currentUserEmail',
                        payload: string};

Literal Type - Flow

© www.soinside.com 2019 - 2024. All rights reserved.