Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Person
✓ import type { Person } from 'schema-dts';
✗ import { Person } from 'schema-dts';
All imports are type-only for Schema.org definitions. Using 'import' without 'type' is not strictly wrong but can lead to unnecessary bundle size increases if a bundler doesn't strip it.
WithContext
✓ import type { WithContext } from 'schema-dts';
✗ const { WithContext } = require('schema-dts');
schema-dts is an ESM-first package shipping only TypeScript types. CommonJS `require` is not supported for these type imports. Use `import type`.
Graph
✓ import type { Graph } from 'schema-dts';
Explicitly import `Graph` when defining JSON-LD with interconnected nodes using the `@graph` property. Prior to v0.9.0, `WithContext<T>` could implicitly be a `Graph`.
MergeLeafTypes
✓ import type { MergeLeafTypes, ProductLeaf, SoftwareApplicationLeaf } from 'schema-dts';
Used for combining multiple concrete Schema.org types into a single JSON-LD object. Always import with `type` and typically alongside `Leaf` types.
Demonstrates defining Schema.org types like `Person` and `Organization` using `WithContext<T>` and then serializing them into a JSON-LD script tag for web embedding.
import type { Person, Organization, Thing, WithContext } from 'schema-dts';
/**
* Helper function to generate a JSON-LD script tag.
* In a real-world app, you might use a templating engine or a UI framework component.
*/
export function generateJsonLdScript<T extends Thing>(json: WithContext<T>): string {
return `<script type="application/ld+json">\n${JSON.stringify(json, null, 2)}\n</script>`;
}
// Define a person's structured data
const inventor: Person = {
'@type': 'Person',
name: 'Grace Hopper',
disambiguatingDescription: 'American computer scientist',
birthDate: '1906-12-09',
deathDate: '1992-01-01',
awards: [
'Presidential Medal of Freedom',
'National Medal of Technology and Innovation',
'IEEE Emanuel R. Piore Award'
]
};
// Define an organization's structured data with context
const myOrganizationLd = generateJsonLdScript<Organization>({
'@context': 'https://schema.org',
'@type': 'Corporation',
name: 'Example Corp LLC',
url: 'https://www.example.com',
logo: 'https://www.example.com/logo.png'
});
console.log(myOrganizationLd);
/* Expected output (prettified JSON):
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Corporation",
"name": "Example Corp LLC",
"url": "https://www.example.com",
"logo": "https://www.example.com/logo.png"
}
</script>
*/
Errors
Common errors & fixes
Type '{
'@context': "https://schema.org";
'@graph': ({ '@type': "Person"; '@id': string; name: string; hasOccupation: { '@type': "Occupation"; name: string; qualifications: string; }; } | { '@type': "WebPage"; '@id': string; name: string; about: { '@id': string; }; author: { '@id': string; }; dateModified: string; datePublished: string; } | { ...; })[];
}' is not assignable to type 'WithContext<T>'.
Attempting to assign a JSON-LD `@graph` object to the `WithContext<T>` type.
fixFor JSON-LD structures utilizing the `@graph` property, import and use the `Graph` type explicitly instead of `WithContext<T>`: `import type { Graph } from 'schema-dts;`. Declaration emit for this file requires using TypeScript version 4.1 or later. The current version is 3.9.10.
Your project's TypeScript version is older than the minimum required by `schema-dts`.
fixUpdate your TypeScript dependency to version 4.1.0 or higher. For example, `npm install -D typescript@latest`.
Audit
Dependencies
No dependency data recorded yet.