score:1
Accepted answer
unfortunately, you cannot force map to have non-undefined return type. so you need to replace it with a simple object and assign a record
type to it.
also i would recommend you to create a type union colormapkeys
where you should define all possible keys.
type colormapkeys = 'standard' | 'accent' | 'neutral' | 'flat' | 'text' | 'disabled';
type colormap = record<colormapkeys, colorsinterface>;
const colormap: colormap = {
'standard': { bgcolor: 'primarynormal', textcolor: 'white' },
'accent': { bgcolor: 'accentnormal', textcolor: 'white' },
'neutral': { bgcolor: 'grayextradark', textcolor: 'white' },
'flat': { bgcolor: 'white', textcolor: 'primarynormal' },
'text': { bgcolor: 'white', textcolor: 'grayextradark' },
'disabled': { bgcolor: 'graylight', textcolor: 'graydark' },
};
export const buttonwrapper = styled.button<buttoninterface>`
${(props: props) =>
props.buttontype &&
css`
background-color: ${props.theme.colors[colormap[props.buttontype].bgcolor]};
color: ${colormap[props.buttontype].textcolor};
`}
`;
now everything will work fine
Source: stackoverflow.com
Related Query
- Type 'undefined' cannot be used as index type
- Type '() => Promise<AxiosResponse<any>>' cannot be used as an index type [ReactJs]
- Type 'String' cannot be used as an index type when adding key/val pair to object
- Typescript: Type 'undefined' cannot be used as an index type.ts(2538)
- Type 'undefined' cannot be used as an index type.ts(2538)
- Typescript error Type 'undefined' cannot be used as an index type
- Type 'undefined' cannot be used as an index type
- React with yup error Type 'undefined' cannot be used as an index type. TS2538
- 'string' can't be used to index type '{}'
- Component cannot be used as a JSX component. Its return type 'Element[]' is not a valid JSX element
- React - cannot be used as a JSX component. Its return type 'void' is not a valid JSX element
- D3 pie chart: Uncaught Type Error - Cannot read property 'pie' of undefined
- Uncaught type error: cannot read property persist of undefined with react-bootstrap carousel
- TS | Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Record<SecurityMode, boolean>'
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type
- TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type
- Type Error: Cannot read property 'map' of undefined in reactjs
- React / NextJS: Type error: 'Component' cannot be used as a JSX component
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Palette'
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type React Typescript
- How to solve: Type error: 'Draggable' cannot be used as a JSX component ? (react-draggable)
- Type error: Cannot read property 'configure' of undefined
- Type Error: Cannot convert undefined or null to object
- Get Type Error: Cannot read property of undefined
- Type Error, cannot read property 'map' of undefined
- Redux combineReducers Uncaught Type error: Cannot convert undefined or null to object
- Render a nested object from API - Type Error: Cannot read properties of undefined
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{...}'
- Type error: Cannot read property currentTime of undefined
More Query from same tag
- How to add text + icon to HTML table row element in Reactjs
- How to get Material-UI Drawer to 'squeeze' other content when open
- Why a class is not being loaded correctly in React?
- Framer Motion exit and AnimatePresence not working in Nextjs?
- How to use map()?
- How can i apply style only to the clicked element [React]
- How to fix TypeError is not a function (testing promises with Jest)
- Not able to access array elements in react JS
- Unable to render two components from App.js
- Loop through objects containing array of objects in reactjs
- Rendering an array of objects in react
- getItem from localStorage does not shows all the data
- React - do I need Flux/Redux at this point? (communication between components)
- React delete list of component with button
- map an array to table where one of those column should be mapped with another array
- Using value in state as component text
- I need to have some fields in ui-schema which do not exist in schema section
- Inner flexbox overrules flex-grow on outer flexbox
- Fetch() is not returning data from server in response
- How to return a list according to selected item?
- Papa parse is giving type error Object possibly null for File type input
- Displaying a nested data with a conditional: How can I not display the data whose product's color is not according to the conditional?
- Whats the best way to add an SVG to ReactJS
- React useState update without set throught child component
- how to show pincodes in sorted order on blur in react?
- How can I enable Hot Module Replacement(HMR) in react?
- what is the difference between getDefaultProps and getInitialState react js
- Amcharts 4 : Disable single legend item programmatically
- I am not getting the list item in my React-app
- How to post with Axios in React?