Schema.org TypeScript Type Definitions
schema-dts provides comprehensive TypeScript definitions for the Schema.org vocabulary, specifically designed for use with JSON-LD structured data. It aims to simplify the creation and validation of Schema.org markup by offering complete sets of discriminated type unions, enabling robust type-checking and IDE auto-completion. The package is currently at version 2.0.0 and frequently updates its typings to align with the latest Schema.org releases, such as v30, v28, v15, ensuring developers always have access to current definitions. A key differentiator is its strict type validation, which helps prevent common errors in JSON-LD implementation. Although hosted under the Google GitHub organization, the README clarifies it is not an officially supported Google product, but it is actively maintained.
Common errors
-
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>'.cause 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.
cause 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`.
Warnings
- breaking Since `schema-dts` v0.10.0, a minimum TypeScript version of 4.1.0 is required. Older TypeScript versions will encounter compilation errors.
- breaking As of `schema-dts` v0.9.0, the `WithContext<T>` type can no longer represent a `Graph`. If you were using `WithContext<T>` to define a `{'@context': foo, '@graph': []}` structure, you must now explicitly import and use the `Graph` type.
- gotcha All imports from `schema-dts` should be type-only imports using `import type { ... } from 'schema-dts';`. While `import { ... } from 'schema-dts';` might work, it's less explicit and can potentially impact bundle size if not properly tree-shaken by your bundler.
- gotcha The package is hosted under the Google GitHub organization but is explicitly stated as 'not an officially supported Google product'. While actively maintained, this might imply different support and stability guarantees compared to official Google products.
Install
-
npm install schema-dts -
yarn add schema-dts -
pnpm add schema-dts
Imports
- Person
import { Person } from 'schema-dts';import type { Person } from 'schema-dts'; - WithContext
const { WithContext } = require('schema-dts');import type { WithContext } from 'schema-dts'; - Graph
import type { Graph } from 'schema-dts'; - MergeLeafTypes
import type { MergeLeafTypes, ProductLeaf, SoftwareApplicationLeaf } from 'schema-dts';
Quickstart
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>
*/