简体   繁体   English

'ItemIsLoading'on指的是一种类型,但此处被用作值

[英]'ItemIsLoading' on refers to a type, but is being used as a value here

I am in the process of building a typescripted Redux store for a react project. 我正在为一个React项目构建一个打字稿的Redux存储。 The Interfaces seem to be working fine, but when I try to pass them within the exports themselves. 接口似乎运行良好,但是当我尝试在导出本身中传递它们时。 I receive the following error "'ItemIsLoading' on refers to a type, but is being used as a value here" 我收到以下错误“'ItemIsLoading'指向类型,但在此处被用作值”

Actions: 操作:

import * as constants from '../constants/constantsIndex';

    export interface ItemHasErrored {
        type: constants.ITEMS_HAS_ERRORED;
    }

    export interface ItemIsLoading {
        type: constants.ITEMS_IS_LOADING;
    }

    export interface ItemFetchDataSuccess {
        type: constants.ITEMS_FETCH_DATA_SUCCESS;
    }

    export function itemsFetchData(url: any) {
        return (dispatch) => {
            dispatch(ItemIsLoading(true));

            fetch(url)
                .then((response) => {
                    if (!response.ok) {
                        throw Error(response.statusText);
                    }

                    dispatch(ItemIsLoading(false));

                    return response;
                })
                .then((response) => response.json())
                .then((items) => dispatch(ItemFetchDataSuccess(items)))
                .catch(() => dispatch(ItemHasErrored(true)));
        };
    }

Constants: 常量:

export const ITEMS_HAS_ERRORED = 'ITEMS_HAS_ERRORED';
export type ITEMS_HAS_ERRORED = typeof ITEMS_HAS_ERRORED;

export const ITEMS_IS_LOADING = 'ITEMS_IS_LOADING';
export type ITEMS_IS_LOADING = typeof ITEMS_IS_LOADING;

export const ITEMS_FETCH_DATA_SUCCESS = 'ITEMS_FETCH_DATA_SUCCESS';
export type ITEMS_FETCH_DATA_SUCCESS = typeof ITEMS_FETCH_DATA_SUCCESS;

For the moment I am only attempting to build the typescript/redux store, but cannot seem to find any information on the nature of this issue. 目前,我仅尝试构建typescript / redux存储,但似乎找不到有关此问题性质的任何信息。 How would someone go about resolving this error? 有人将如何解决该错误?

Interface in typescript can't be instantiated, they can be extended though. Typescript中的接口无法实例化,但是可以扩展。 So, if you wish to use an instance that matches the type signature of an interface, you should extend it. 因此,如果您希望使用与接口的类型签名匹配的实例,则应该对其进行扩展。

You can implement the interfaces using a class 您可以使用类来实现接口

class ItemIsLoadingAction implements ItemIsLoading {
  payload: boolean;
  constructor(payload) {
    this.payload = payload;
  }
}

Then, you can use it's instance to dispatch the action like 然后,您可以使用它的实例来调度操作,例如

dispatch(new ItemIsLoadingAction(false))

This can be applicable for the rest of the interfaces. 这可以适用于其余接口。

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

相关问题 指的是一个值,但在这里被用作一个类型 - refers to a value, but is being used as a type here 元素指的是一个值,但在这里被用作类型 - element refers to a value, but is being used as a type here 导出时类型“仅指一种类型,但在此处用作值” - Type “only refers to a type, but is being used as a value here” when exporting “承诺”仅指类型,但在此处用作值 - 'Promise' only refers to a type, but is being used as a value here “'输出'仅指一种类型,但在此处使用了一个值”错误 - "'Output' only refers to a type, but is being used a value here" error “post”仅指一种类型,但在此处用作值 - 'post' only refers to a type, but is being used as a value here ionic 'DocumentScanner' 指的是一个值,但在这里被用作一个类型 - ionic 'DocumentScanner' refers to a value, but is being used as a type here 'MapEditServiceConfig'仅引用类型,但在此处用作值 - 'MapEditServiceConfig' only refers to a type, but is being used as a value here IFormContext 只引用一个类型,但在这里用作一个值 - IFormContext only refers to a type, but is used as a value here 为什么 TypeScript 中的 'instanceof' 给我错误“'Foo' 仅指一种类型,但在此处用作值。”? - Why does 'instanceof' in TypeScript give me the error "'Foo' only refers to a type, but is being used as a value here."?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM