Phillip YS
Phillip YS

Reputation: 904

Typescript: Type 'string' is not assignable to custom type

I have a type like this,

interface StyleProps {
  display?: 'hide' | 'active'
}

and the type is being used by component below

<Section display={`${this.state.section !== 'chatbot' ? 'hide' : 'active'}`}>

display can only be hide or active but I'm still getting this error

 TS2322: Type 'string' is not assignable to type '"hide" | "active" | undefined'.

Is there any way to see what's in display or anything im doing it wrong?

Upvotes: 1

Views: 594

Answers (1)

Tholle
Tholle

Reputation: 112787

You can get rid of this error if you remove the template literal and just pass in either 'hide' or 'active' directly.

<Section display={this.state.section !== 'chatbot' ? 'hide' : 'active'}>

Upvotes: 2

Related Questions