Assign a variable to Button themeColor issues using typescript

1 Answer 59 Views
Button
Patrick
Top achievements
Rank 1
Iron
Patrick asked on 06 Mar 2024, 03:49 PM | edited on 06 Mar 2024, 03:58 PM

I am trying to set the button themeColor attribute to a variable such as:

let themeColor: string = 'dark';

themeColor={themeColor}

but I get the error:

 

Type 'string' is not assignable to type '"base" | "dark" | "success" | "error" | "warning" | "info" | "primary" | "secondary" | "tertiary" | "light" | "inverse" | null | undefined'.ts(2322)
Button.d.ts(70, 5): The expected type comes from property 'themeColor' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<Button> & Pick<Pick<Readonly<ButtonProps>, "form" | ... 283 more ... | "onTransitionEndCapture"> & Pick<...> & Pick<...>, "form" | ... 279 more ... | "onTransitionEndCapture"> & InexactPartial<...> & InexactPartial<...>'
(property) themeColor?: "base" | "dark" | "success" | "error" | "warning" | "info" | "primary" | "secondary" | "tertiary" | "light" | "inverse" | null | undefined

1 Answer, 1 is accepted

Sort by
0
Patrick
Top achievements
Rank 1
Iron
answered on 06 Mar 2024, 05:45 PM

I figured it out. I needed to define a type ThemeColor as below:

type ThemeColor = ("success" | "error" | "warning" | "info" | "base" | "primary" | "secondary" | "tertiary" | "dark" | "light" | "inverse" | null | undefined): 
.

.

.const themeColor: ThemeColor = 'dark';

.

.

.

themeColor={themeColor}

Tags
Button
Asked by
Patrick
Top achievements
Rank 1
Iron
Answers by
Patrick
Top achievements
Rank 1
Iron
Share this question
or