--- import SchemaOrg from "./SchemaOrg.astro"; export interface FAQItem { question:…
- User
import SchemaOrg from "./SchemaOrg.astro";
export interface FAQItem { question: string; answer: string; }
export interface Props { title?: string; items: FAQItem[]; }
const { title = "Frequently Asked Questions", items } = Astro.props;
<div class="max-w-3xl mx-auto divide-y divide-gray-200 dark:divide-gray-700" > { items.map((item, index) => ( <div class="py-5 faq-item"> <button class="flex w-full justify-between items-center text-left focus:outline-none" aria-expanded="false" aria-controls={`faq-${index}`} > <h3 class="font-heading font-semibold text-lg pr-6"> {item.question} </h3> <span class="flex-shrink-0 ml-2"> <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 transform transition-transform duration-200 faq-icon" fill="none" viewBox="0 0 24 24" stroke="currentColor" > <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7" /> </svg> </span> </button> <div id={`faq-${index}`} class="mt-2 text-gray-600 dark:text-gray-400 faq-content hidden" > <p set:html={item.answer} /> </div> </div> )) } </div><SchemaOrg type="faq" data={{ questions: items, }} />
Property 'style' does not exist on type 'Element'
Assistant
The TypeScript error you're seeing occurs because
querySelectorreturns anElementtype, but thestyleproperty is only available onHTMLElement. You need to cast the result toHTMLElementor use a more specific selector method.Here's the corrected script section:
<script> // Simple accordion functionality document.addEventListener("DOMContentLoaded", () => { const accordionButtons = document.querySelectorAll(".faq-item button"); accordionButtons.forEach((button) => { const controlId = button.getAttribute("aria-controls"); const content = controlId ? document.getElementById(controlId) : null; button.addEventListener("click", () => { const expanded = button.getAttribute("aria-expanded") === "true"; button.setAttribute("aria-expanded", (!expanded).toString()); if (content) { content.classList.toggle("hidden"); } // Toggle icon rotation const icon = button.querySelector(".faq-icon") as HTMLElement; if (icon) { icon.style.transform = expanded ? "" : "rotate(180deg)"; } }); }); }); </script>The key change is adding
as HTMLElementafterbutton.querySelector(".faq-icon"). This type assertion tells TypeScript that the element is an HTMLElement, which has thestyleproperty.Alternatively, you could use a type guard:
const icon = button.querySelector(".faq-icon"); if (icon instanceof HTMLElement) { icon.style.transform = expanded ? "" : "rotate(180deg)"; }Both approaches will resolve the TypeScript error while maintaining the functionality of your FAQ accordion component.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.