Closed AeonFr closed 3 years ago
Thanks for reporting this. I think I'll need to see the exact signature of getArticleItemStructuredData
. I can't repro this on my own:
// typescript 4.5.3
// schema-dts 0.8.3
import { WithContext, ItemList, Article } from 'schema-dts';
const articleList: string[] = [];
function getArticleItemStructuredData({
articleItem: string,
position: number
}): Article {
return {
'@type': 'Article'
};
}
const list: WithContext<ItemList> = {
'@context': 'https://schema.org',
'@type': 'ItemList',
itemListElement: articleList.map((articleItem, i) =>
getArticleItemStructuredData({
articleItem,
position: i + 1
})
)
};
The error, "ItemList"' is not assignable to type '"OfferCatalog"
, sounds like a red herring. But it might be because your itemListElement
type mismatched, and TS wasn't sure which type union item to complain about.
Looks like the error comes from the fact that I'm returning WithContext<Article>
instead of Article
in the getArticleItemStructuredData
function.
Previously this didn't raised any issues, but now it does.
Declaring @context at multiple levels is not considered "invalid" by the google validation tools, so we rolled with it for simplicity. But I understand that it's not the intended way to build schemas.
Thanks for the help. And thanks for the good work maintaining this library
Great to hear this was resolved.
I see how this would have happened:
WithContext<T>
to T
should have worked.0.7.4 added support for a @graph
nodes, changing the type of WithContext
:
- export declare type WithContext<T extends Thing> = T & {
+ export declare type WithContext<T extends Thing> = Graph | (T & {
"@context": "https://schema.org";
+ });
- };
+ export interface Graph {
+ "@context": "https://schema.org";
+ "@graph": readonly Thing[];
+ }
and TypeScript now correctly notes that Graph
isn't assignable to Article
.
I think including Graph
in WithContext<>
is a mistake, especially since Graph
is exported itself as-is. But now I'm not sure why I did that to begin with.
Fixed in 0.9.0
After updating from 0.7 to 0.8, I get errors with this code:
The property
@type
gives the following error:Type '"ItemList"' is not assignable to type '"OfferCatalog"'
I would be able to bypass this error by importing and using
ItemListLeaf
instead ofItemList
, but this interface is not exported. For now my workaround was to recreate the interface (based on the sourcecode, I useOmit<>
to deny all members exceptItemListLeaf
from the intersection).Notice that when I run the output of this code in the Rich Results Test or in the Structured Data Testing Tool, I don't get any errors, that's why I think the problem might not be in my original interface, but in the way the typings are built.