I am working with typescript and I encountered an error Property 'splice' does not exist on type 'T'.
type Item = {
name: string,
body: string,
imgOne: string,
imgTwo: string,
}[]
// In a different file, this function is present
interface Props<T> {
className?: string;
items: T
setItems: Dispatch<SetStateAction<T>>;
children?: ReactNode
}
export const Carousel = <T extends object>({className, items, setItems, children}: Props<T>) => {
console.log(items.splice(0,2));
return <div />
}
// I am calling this function in a 3rd file.
export const HomeCarousel: FC = () => {
const {Root} = useStyles();
const classes = useStylesClasses();
const [items, setItems] = useState<Item[]>([
{
name: "Naruto1",
body: "Dive into the world of shinobi to witness one of the greatest tales",
imgOne: "/two.svg",
imgTwo: "/one.svg",
},
{
name: "Naruto2",
body: "Dive into the world of shinobi to witness one of the greatest tales",
imgOne: "/two.svg",
imgTwo: "/one.svg",
},
{
name: "Naruto3",
body: "Dive into the world of shinobi to witness one of the greatest tales",
imgOne: "/two.svg",
imgTwo: "/one.svg",
},
]);
return (
<Root>
<Carousel<Item[]> setItems={setItems} items={items}>
{items.map((item, index)=> {
return <CarouselItem name={item.name}/>
})}
</Carousel>
</Root>
)
}
The splice function isn't working, now on one hand it makes sense since there is no splice in the type Item but shouldn't it work by default since it is an array?
Any help is appreciated!