3
votes

When I add itemscope itemtype="http://schema.org/Product" to h1, I get this error:

Type '{ children: string; itemscope: true; itemtype: string; }' is not assignable to type 'DetailedHTMLProps<HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>'. Property 'itemscope' does not exist on type 'DetailedHTMLProps<HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>'

<h1 itemscope itemtype="http://schema.org/Product">Amasia</h1>

How to use Microdata in React+Typescript?

3
React itself supports this, as of version 16. The issue is with the TypeScript definitions, it seems.Joe Clay

3 Answers

1
votes

Not very elegant, but this works:

// foo.d.ts
declare namespace React {
    interface HTMLAttributes<T> {
        itemscope?: boolean;
        itemtype?: string;
    }
}
// app.tsx
function foo(): JSX.Element {
    return (
        <h1 itemscope itemtype="http://schema.org/Product">
            Amasia
        </h1>
    );
}

Works for me (Typescript 3.4.5).

1
votes

For React, it is case Sensitive. Pay attention to: itemScope and itemType spelling

<div itemScope itemType={"http://schema.org/Product"}>{...}</div>
0
votes

If you use typescript you should write like this

<h1 itemScope itemType={"http://schema.org/Product"}>{...}</h1>