仅指一种类型,但在此处用作值

Posted

技术标签:

【中文标题】仅指一种类型,但在此处用作值【英文标题】:only refers to a type, but is being used as a value here 【发布时间】:2018-08-07 06:58:48 【问题描述】:

我在编写这个 React Native 应用程序时使用了 TypeScript。 尝试创建一个简单的操作,将字符串作为参数并更新状态。但是我得到了这个我不明白的错误。

src/containers/DamageClaimReceipt.tsx

import * as React from "react"
import  Text, View  from "react-native"
import  Question  from "../components/Question"
import GjeButton from "../components/GjeButton"
import  bindActionCreators, Dispatch  from "redux"
import  connect  from "react-redux"
import 
  SetDamageClaimReceiptNrAction
 from "../actions/processguide"

const mapDispatchToProps = (dispatch: Dispatch<any>) => bindActionCreators(
  setDamageClaimReceiptNr: SetDamageClaimReceiptNrAction,
  // 'SetDamageClaimReceiptNrAction' only refers to a type, 
  // but is being used as a value here.
, dispatch)

class DamageClaimReceipt extends React.Component<any, any> 
  public render() 
    return (
      <View>

       <GjeButton
        title="Set DamageClaimReceiptNr"
        onPress= () => this.props.setDamageClaimReceiptNr("100") 
      />

      </View>
    )
  


export default connect<null, null>(
  null,
  mapDispatchToProps,
)(DamageClaimReceipt)

src/actions/processguide.ts

 10 export type SetDamageClaimReceiptNrAction =
 11   (damageClaimReceiptNr: string) => ISetDamageClaimReceiptNrAction
 12
 13 export interface ISetDamageClaimReceiptNrAction 
 14   type: actionTypes.SET_PROCESSGUIDE_RECEIPT_NR_FOR_CLAIM
 15   receiptNrForClaim: string
 16 

【问题讨论】:

【参考方案1】:

SetDamageClaimReceiptNrAction定义了一个方法签名,当调用bindActionCreators时你传递一个对象字面量给方法,带有setDamageClaimReceiptNr属性,该属性不能有函数签名的值,它可以是一个函数但是。

const mapDispatchToProps = (dispatch: Dispatch<any>) => bindActionCreators(
  setDamageClaimReceiptNr: (damageClaimReceiptNr: string) => (
       type: actionTypes.SET_PROCESSGUIDE_RECEIPT_NR_FOR_CLAIM
       receiptNrForClaim: "Value"
  ),
, dispatch)

【讨论】:

以上是关于仅指一种类型,但在此处用作值的主要内容,如果未能解决你的问题,请参考以下文章

打字稿:错误 TS2693:“承诺”仅指一种类型,但在此处用作值

打字稿错误:“”仅指一种类型,但在此处用作命名空间

为啥 TypeScript 中的 'instanceof' 会给我错误“'Foo' 仅指一种类型,但在这里被用作值。”?

ReactJS 和 Typescript :引用一个值,但在此处用作类型 (TS2749)

将 Require 语句替换为 Import 语句导致错误

“导航栏指的是一个值,但在此处用作类型”在测试时尝试呈现我的组件的浅表副本