Walter White
Walter White

Reputation: 1026

Cast to map object

I have error

Type 'string' is not assignable to type '() => string'

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

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

this is are default initialized value for "selectedValue"

What I should do to headerOcr.author.profession? display; () => '' should have const type.

Upvotes: 2

Views: 123

Answers (2)

P.S.
P.S.

Reputation: 16384

Type 'string' is not assignable to type '() => string'

Typescript expect that all of your object properties will be strings, so there are two ways:

  1. Return value in display: () => return '',
  2. Change your IDocumentType interface like this:

IDocumentType { /*...*/ display: () => string /*...*/ }

Upvotes: 2

Murat Karagöz
Murat Karagöz

Reputation: 37604

You can not pass a function if you defined it as a string type. Either you change the definition of headerOcr or evaluate the function call before passing it to display.

The change of definition would look like these:

IDocumentType {
  display: () => string
}

With that you have to use it as IDocumentType.display()

Upvotes: 2

Related Questions