score:0
Accepted answer
I think there was a bug in the code as I just copied it from someone's JS project. TS made me catch the error.
I changed my code to check if gray
exists on codeBackground[color]
& it got rid of the error.
Here's the code:
import clsx from 'clsx'
type BGColors = {
amber: string
emerald: string
fuchsia: string
gray: string
indigo: string
lightBlue: string
purple: string
rose: string
}
const codeBackground: Omit<BGColors, 'gray'> = {
amber: 'bg-amber-500',
emerald: 'bg-emerald-500',
fuchsia: 'bg-fuchsia-400',
// gray: 'bg-gray-400',
indigo: 'bg-indigo-400',
lightBlue: 'bg-light-blue-500',
purple: 'bg-purple-400',
rose: 'bg-rose-400',
}
const previewBackground: BGColors = {
amber: 'bg-gradient-to-r from-amber-50 to-amber-100',
emerald: 'bg-gradient-to-r from-emerald-50 to-teal-100',
fuchsia: 'bg-gradient-to-r from-fuchsia-50 to-fuchsia-100',
gray: 'bg-gradient-to-r from-gray-50 to-gray-100',
indigo: 'bg-gradient-to-r from-indigo-50 to-indigo-100',
lightBlue: 'bg-gradient-to-r from-light-blue-50 to-light-blue-100',
purple: 'bg-gradient-to-r from-purple-50 to-purple-100',
rose: 'bg-gradient-to-r from-rose-50 to-rose-100',
}
type CodeSampleProps = {
preview: string
snippet: string
previewClassName: string
color: keyof BGColors
}
export const CodeSample = ({
preview,
snippet,
previewClassName,
color = 'gray',
}: CodeSampleProps) => {
return (
<div className="relative mb-8 overflow-hidden">
<div
className={clsx(
'rounded-t-xl overflow-hidden',
previewBackground[color],
previewClassName,
{
'p-10': !previewClassName,
}
)}
dangerouslySetInnerHTML={{ __html: preview }}
/>
<div
className={clsx(
'overflow-hidden rounded-b-xl',
color === 'gray' ? '' : codeBackground[color],
{
'bg-gray-800': color === 'gray' ? '' : !codeBackground[color],
}
)}
>
<pre
className={clsx(
'scrollbar-none overflow-x-auto p-6 text-sm leading-snug language-html text-white',
{
'bg-black bg-opacity-75': color === 'gray' ? '' : codeBackground[color],
}
)}
>
<code className="language-html" dangerouslySetInnerHTML={{ __html: snippet }} />
</pre>
</div>
</div>
)
}
Source: stackoverflow.com
Related Query
- react typescript error - element implicitly has an any type because expression of type string cant be used to index type {}
- 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
- 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
- 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 '{...}'
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Breakpoints'
- how fix Element implicitly has an 'any' type because expression of type 'string' can't be used to index type?
- Global window object: Element implicitly has an 'any' type because index expression is not of type 'number'.ts
- TypeScript - Element implicitly has an 'any' type because expression of type '"storyFile"' can't be used to index type '{}'
- Typescript with React > Element implicitly has an 'any' type because expression of type 'string' can't be used to index
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'Item'
- Nested Object -> Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{}' - React Anagram
- Element implicitly has an 'any' type because expression of type 'string | number' can't be used to index type
- error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index 'ModuleType'
- Element implicitly has an 'any' type because expression of type 'number' can't be used to index type 'Number'
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'
- Element implicitly has an 'any' type because expression of type 'number' can't be used to index type '{}'
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ 1: Element; 2: Element; 3: Element; }'. TS7053
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type with createStyles
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'BGColors'
- Typescript error in map(): Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
- error TS2602: JSX element implicitly has type 'any' because the global type 'JSX.Element' does not exist
- TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type
- Typescript-React State: Element implicitly has an 'any' type because type 'State' has no index signature
- How do I fix the error "Element implicitly has an 'any' type because the expression of type
- React Typescript: Element implicitly has an 'any' type because type has no index signature
- element implicitly has an 'any' type because type 'yyy' has no index signature.ts(7017)
More Query from same tag
- setState unexpectedly updates non-state properties
- How to make two Component connect each other on React-Redux
- how to use useState in an html tag?
- How can I get a width of html element in React JS?
- How to show image or anything else in Kendo grid title with React
- Excessive rerendering when interacting with global state in React Context
- React + TS - pass parameters with redirect
- sidebar with Reactjs
- Can I edit React components without reloading the browser?
- How does the React component's state changes by using [event.target.name]?
- Invalid plugin options for "gatsby-source-contentful"
- How can I redirect facebook crawler bot to another webpage?
- Passing props within stateless functional components
- How to export a variable (its a list actually) to other .js file in a function inside class
- Jest mocking fetch() function that get the blob of the response
- TypeError: Cannot read property '0' of null in react
- TypeScript - Combine two interfaces and making some of its members mandatory
- Typescript Date Timezone setting
- Can't render out an array inside the data of my state
- Debugging es6 in create-react-app
- Wait until onSnapshot data is fetched Firestore Firebase Next JS
- Debugging To many rerenders error react redux
- Destructuring binding to a function?
- Adding an item to react state dictionary
- React setTimeout calling order
- React component state passed as prop is undefined in Jest test
- How to set an interface from object values avoiding string type
- React component error message using filter method
- nextjs react recoil persist values in local storage: initial page load in wrong state
- How to correctly style and apply hover for infinitely nested nav in React?