转换为地图对象

时间:2017-09-21 10:58:07

标签: javascript typescript

我有错误

  

键入' string'不能分配给'()=>字符串'

this.selectedValue.display = headerOcr.author.profession;

private selectedValue: IDocumentType = {
    id: '1',
    display: () => '',
    label: ''
}

这是" selectedValue"

的默认初始化值

我应该对headerOcr.author.profession做些什么? display; () => ''应该有const类型。

2 个答案:

答案 0 :(得分:2)

如果您将函数定义为string类型,则无法传递函数。要么更改headerOcr的定义,要么在将函数调用传递给显示之前对其进行评估。

定义的变化如下:

IDocumentType {
  display: () => string
}

有了它,你必须将其用作IDocumentType.display()

答案 1 :(得分:2)

  

键入' string'不能分配给'()=>字符串'

Typescript期望所有对象属性都是字符串,因此有两种方法:

  1. display: () => return '',
  2. 中的返回值
  3. 像这样更改您的IDocumentType界面:
  4. IDocumentType { /*...*/ display: () => string /*...*/ }

相关问题