### Install project dependencies Source: https://github.com/google/schema-dts/blob/main/README.md Install all necessary dependencies for the project using npm. ```sh npm install ``` -------------------------------- ### Install schema-dts-gen Source: https://github.com/google/schema-dts/blob/main/README.md Install the schema-dts-gen package as a development dependency using npm. ```sh npm install --save-dev schema-dts-gen ``` -------------------------------- ### Install and Run schema-dts-gen Source: https://github.com/google/schema-dts/blob/main/packages/schema-dts-gen/README.md Installs the schema-dts-gen package as a development dependency and then runs it to generate typings from a specified ontology URL. ```sh npm install --save-dev schema-dts-gen npx schema-dts-gen --ontology=https://schema.org/version/latest/schemaorg-all-https.nt ``` -------------------------------- ### Install schema-dts Package Source: https://github.com/google/schema-dts/blob/main/README.md Install the schema-dts NPM package as a development dependency. ```sh npm install --save-dev schema-dts ``` -------------------------------- ### React Product Schema Integration Source: https://context7.com/google/schema-dts/llms.txt Uses the `react-schemaorg` library to embed Product schema in a React component. Ensure `react-schemaorg` is installed. ```tsx import {JsonLd} from 'react-schemaorg'; import type {Product} from 'schema-dts'; export function ProductPage() { return ( item={{ '@context': 'https://schema.org', '@type': 'Product', name: 'Classic Leather Wallet', offers: {'@type': 'Offer', price: 89, priceCurrency: 'USD'}, }} /> ); } ``` -------------------------------- ### React: Inject JSON-LD with react-schemaorg Source: https://github.com/google/schema-dts/blob/main/examples.md Use the `` component from `react-schemaorg` for XSS-safe JSON-LD serialization in React applications. Ensure `react-schemaorg` is installed. ```tsx import {JsonLd} from 'react-schemaorg'; import type {Product} from 'schema-dts'; export function ProductPage() { return ( item={{ '@context': 'https://schema.org', '@type': 'Product', name: 'Classic Leather Wallet', offers: { '@type': 'Offer', price: 89, priceCurrency: 'USD', }, }} /> ); } ``` -------------------------------- ### Define Simple Person Properties with schema-dts Source: https://github.com/google/schema-dts/blob/main/packages/schema-dts/README.md Example of defining a 'Person' object with various properties like name, birthDate, and awards using schema-dts types. ```typescript import type {Person} from 'schema-dts'; 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 WebSite Schema Source: https://github.com/google/schema-dts/blob/main/README.md Create a basic `WebSite` schema with name and URL. Use `WithContext` to include the JSON-LD context. ```typescript import type {WebSite, WithContext} from 'schema-dts'; const site: WithContext = { '@context': 'https://schema.org', '@type': 'WebSite', name: 'Acme Corp', url: 'https://acme.com', }; ``` -------------------------------- ### Define Product Schema with Offer Source: https://github.com/google/schema-dts/blob/main/README.md Structure a `Product` schema including details like name, description, image, SKU, brand, and offer information. Use `WithContext` for the JSON-LD context. ```typescript import type {Product, WithContext} from 'schema-dts'; const product: WithContext = { '@context': 'https://schema.org', '@type': 'Product', name: 'Classic Leather Wallet', description: 'Full-grain leather bifold wallet with RFID blocking.', image: 'https://shop.example/images/wallet.jpg', sku: 'WALLET-001', brand: { '@type': 'Brand', name: 'Example Shop', }, offers: { '@type': 'Offer', price: 89, priceCurrency: 'USD', availability: 'https://schema.org/InStock', url: 'https://shop.example/products/classic-wallet', }, }; ``` -------------------------------- ### Generate typings from a local .nt file Source: https://context7.com/google/schema-dts/llms.txt Generate typings from a local NTriples ontology file using the --file flag. ```sh # Use a local .nt file npx schema-dts-gen \ --file=./my-ontology.nt \ > custom-schema.d.ts ``` -------------------------------- ### Generate typings from latest Schema.org core ontology Source: https://context7.com/google/schema-dts/llms.txt Generate typings from the latest Schema.org core ontology and pipe the output to a file. This command uses the default context. ```sh # Generate typings from the latest Schema.org core ontology (writes to stdout) npx schema-dts-gen \ --ontology=https://schema.org/version/latest/schemaorg-all-https.nt \ > schema.d.ts ``` -------------------------------- ### Generate typings with multi-namespace context Source: https://context7.com/google/schema-dts/llms.txt Generate typings using a multi-namespace context, specifying prefixes for RDF schema and Schema.org. ```sh # Multi-namespace context (rdf + schema prefixes) npx schema-dts-gen \ --ontology=https://schema.org/version/latest/schemaorg-all-https.nt \ --context=rdf:http://www.w3.org/2000/01/rdf-schema,schema:https://schema.org \ > schema-prefixed.d.ts ``` -------------------------------- ### Define FAQPage Schema Source: https://github.com/google/schema-dts/blob/main/README.md Create an `FAQPage` schema with a list of questions and their accepted answers. Use `WithContext` for the JSON-LD context. ```typescript import type {FAQPage, WithContext} from 'schema-dts'; const faq: WithContext = { '@context': 'https://schema.org', '@type': 'FAQPage', mainEntity: [ { '@type': 'Question', name: 'Do you ship internationally?', acceptedAnswer: { '@type': 'Answer', text: 'Yes, we ship to over 50 countries.', }, }, { '@type': 'Question', name: 'What is your return policy?', acceptedAnswer: { '@type': 'Answer', text: 'We offer a 30-day return policy on all items.', }, }, ], }; ``` -------------------------------- ### Build Schema Typings Generator and Schema Source: https://github.com/google/schema-dts/blob/main/README.md Execute npm scripts to first build the generator and then generate the Schema.org typings. This is a common workflow for developers contributing to the project. ```sh npm run build-gen && npm run build-schema ``` -------------------------------- ### Generate TypeScript typings from Schema.org Source: https://github.com/google/schema-dts/blob/main/README.md Use npx to run the schema-dts-gen command, specifying the ontology URL to generate TypeScript typings. Ensure the ontology is compatible with Schema.org. ```sh npx schema-dts-gen --ontology=https://schema.org/version/latest/schemaorg-all-https.nt ``` -------------------------------- ### Load ontology quads from a remote URL Source: https://context7.com/google/schema-dts/llms.txt Use the loadTriples function to fetch an NTriples file from an HTTPS URL and parse it into an N3 Store. This function handles HTTP redirects automatically. ```ts import {loadTriples} from 'schema-dts-gen'; // For local files, import from the internal path or use the CLI --file flag. // The programmatic equivalent: import {loadFile} from 'schema-dts-gen/dist/src/triples/reader.js'; // Remote — handles HTTP redirects automatically const remoteStore = await loadTriples( 'https://schema.org/version/latest/schemaorg-all-https.nt' ); console.log(`Loaded ${remoteStore.size} quads`); // Local file const localStore = await loadFile('./ontologies/my-schema.nt'); console.log(`Loaded ${localStore.size} quads from local file`); ``` -------------------------------- ### Define Organization Schema Source: https://github.com/google/schema-dts/blob/main/README.md Create a basic `Organization` schema with essential properties like name, URL, logo, and social media links. Use `WithContext` to include the JSON-LD context. ```typescript import type {Organization, WithContext} from 'schema-dts'; const org: WithContext = { '@context': 'https://schema.org', '@type': 'Organization', name: 'Acme Corp', url: 'https://acme.com', logo: 'https://acme.com/logo.png', sameAs: ['https://twitter.com/acme', 'https://www.linkedin.com/company/acme'], }; ``` -------------------------------- ### Define WebSite with SearchAction Source: https://github.com/google/schema-dts/blob/main/README.md Embed a `SearchAction` within a `WebSite` schema, using `WithActionConstraints` to define its input. ```typescript import type {SearchAction, WebSite, WithActionConstraints} from 'schema-dts'; const website: WebSite = { '@type': 'WebSite', potentialAction: { '@type': 'SearchAction', 'query-input': 'required name=search_term_string', } as WithActionConstraints, }; ``` -------------------------------- ### Build Schema Typings Generator only Source: https://github.com/google/schema-dts/blob/main/README.md Run the npm script specifically to build the schema-dts generator without generating the Schema.org typings. Useful for development of the generator itself. ```sh npm run build-gen ``` -------------------------------- ### Define Article Schema Source: https://github.com/google/schema-dts/blob/main/README.md Structure an `Article` schema with headline, publication dates, author, image, and description. Use `WithContext` for the JSON-LD context. ```typescript import type {Article, WithContext} from 'schema-dts'; const article: WithContext
= { '@context': 'https://schema.org', '@type': 'Article', headline: 'How to choose a leather wallet', datePublished: '2026-03-01', dateModified: '2026-03-15', author: { '@type': 'Person', name: 'Jane Smith', url: 'https://blog.example/authors/jane', }, image: 'https://blog.example/images/wallet-guide.jpg', description: 'A practical guide to choosing a leather wallet that lasts.', }; ``` -------------------------------- ### Generate typings from a specific Schema.org version Source: https://context7.com/google/schema-dts/llms.txt Generate typings from a specific version of the Schema.org ontology by providing the version number in the ontology URL. ```sh # Use a specific Schema.org version npx schema-dts-gen \ --ontology=https://schema.org/version/26.0/schemaorg-all-https.nt \ > schema-26.d.ts ``` -------------------------------- ### Generate typings with verbose output Source: https://context7.com/google/schema-dts/llms.txt Generate typings with verbose output enabled, directing diagnostic logs to stderr and declarations to stdout. ```sh # Verbose output (diagnostic logs on stderr, declarations on stdout) npx schema-dts-gen \ --ontology=https://schema.org/version/latest/schemaorg-all-https.nt \ --verbose \ > schema.d.ts ``` -------------------------------- ### Svelte: Inject JSON-LD into Source: https://github.com/google/schema-dts/blob/main/examples.md Use Svelte's `` to inject JSON-LD into the document's head. The provided `safeJsonLd` function ensures XSS safety by escaping special characters. Import necessary types from `schema-dts`. ```svelte {@html ``} ``` -------------------------------- ### Merge Multiple Schema Types with MergeLeafTypes Source: https://github.com/google/schema-dts/blob/main/packages/schema-dts/README.md Shows how to combine multiple concrete schema types, such as 'Product' and 'SoftwareApplication', into a single type using `MergeLeafTypes` for objects that represent more than one schema type. ```typescript import type { MergeLeafTypes, ProductLeaf, SoftwareApplicationLeaf, WithContext, } from 'schema-dts'; const app: WithContext> = { '@context': 'https://schema.org', '@type': ['Product', 'SoftwareApplication'], name: 'My App', offers: { '@type': 'Offer', price: 89, priceCurrency: 'USD', }, operatingSystem: 'Any', }; ``` -------------------------------- ### Annotate SearchAction with Input Constraints Source: https://github.com/google/schema-dts/blob/main/README.md Use `WithActionConstraints` to add input and output constraints to action types like `SearchAction`. The `query-input` property specifies the required input name. ```typescript import type {SearchAction, WithActionConstraints} from 'schema-dts'; const potentialAction: WithActionConstraints = { '@type': 'SearchAction', 'query-input': 'required name=search_term_string', // ... }; ``` -------------------------------- ### Astro: Inject JSON-LD with inline script tag Source: https://github.com/google/schema-dts/blob/main/examples.md Render JSON-LD directly within an Astro component's template using an inline ``; } export const MY_ORG = JsonLd({ '@context': 'https://schema.org', '@type': 'Corporation', name: 'Google LLC', }); ``` -------------------------------- ### Vanilla TypeScript: Framework-agnostic JSON-LD injection Source: https://github.com/google/schema-dts/blob/main/examples.md Inject JSON-LD directly into the document's head using vanilla TypeScript without relying on any framework. This method involves creating a script element and appending it to the document head. Ensure `schema-dts` types are imported. ```ts import type {WebSite, WithContext} from 'schema-dts'; const site: WithContext = { '@context': 'https://schema.org', '@type': 'WebSite', name: 'Acme Corp', url: 'https://acme.com', }; const script = document.createElement('script'); script.type = 'application/ld+json'; script.textContent = JSON.stringify(site); document.head.appendChild(script); ``` -------------------------------- ### Next.js: Inject JSON-LD with Script component Source: https://github.com/google/schema-dts/blob/main/examples.md Utilize Next.js's built-in ` ``` ``` -------------------------------- ### Graph Source: https://context7.com/google/schema-dts/llms.txt The Graph type provides a typed interface for JSON-LD documents containing multiple, interconnected top-level nodes under a "@graph" key. Each element in the array can be any Schema.org type. ```APIDOC ## Graph — JSON-LD `@graph` with interconnected nodes `Graph` is a typed interface for JSON-LD documents that contain multiple, interlinked top-level nodes under a `"@graph"` key. Each array element is a `Thing` (the top-level union of all Schema.org types) and any node can carry an `"@id"` IRI. Other nodes can reference it with an ID stub `{ '@id': '...' }` (typed as `IdReference`). ### Example Usage ```ts import type {Graph} from 'schema-dts'; const graph: Graph = { '@context': 'https://schema.org', '@graph': [ { '@type': 'Person', '@id': 'https://my.site/#alyssa', name: 'Alyssa P. Hacker', hasOccupation: { '@type': 'Occupation', name: 'LISP Hacker', qualifications: 'Knows LISP', }, mainEntityOfPage: {'@id': 'https://my.site/about/#page'}, subjectOf: {'@id': 'https://my.site/about/#page'}, }, { '@type': 'WebPage', '@id': 'https://my.site/about/#page', url: 'https://my.site/about/', name: "About | Alyssa P. Hacker's Website", inLanguage: 'en-US', about: {'@id': 'https://my.site/#alyssa'}, mainEntity: {'@id': 'https://my.site/#alyssa'}, }, { '@type': 'WebSite', '@id': 'https://my.site/#site', url: 'https://my.site', name: "Alyssa P. Hacker's Website", inLanguage: 'en-US', mainEntity: {'@id': 'https://my.site/#alyssa'}, }, ], }; // Type-checked: unknown property is caught at compile time const badGraph: Graph = { '@context': 'https://schema.org', '@graph': [ { '@type': 'Thing', // @ts-expect-error Unknown property unknownProp: 5, }, ], }; ``` ``` -------------------------------- ### Structure Interconnected Data with schema-dts Graph Source: https://github.com/google/schema-dts/blob/main/packages/schema-dts/README.md Illustrates how to define interconnected JSON-LD nodes using the `Graph` type, including assigning unique `@id`s to nodes and referencing them from other parts of the graph. This is useful for representing complex relationships between entities. ```typescript import type {Graph} from 'schema-dts'; const graph: Graph = { '@context': 'https://schema.org', '@graph': [ { '@type': 'Person', '@id': 'https://my.site/#alyssa', name: 'Alyssa P. Hacker', hasOccupation: { '@type': 'Occupation', name: 'LISP Hacker', qualifications: 'Knows LISP', }, mainEntityOfPage: {'@id': 'https://my.site/about/#page'}, subjectOf: {'@id': 'https://my.site/about/#page'}, }, { '@type': 'AboutPage', '@id': 'https://my.site/#site', url: 'https://my.site', name: "Alyssa P. Hacker's Website", inLanguage: 'en-US', description: 'The personal website of LISP legend Alyssa P. Hacker', mainEntity: {'@id': 'https://my.site/#alyssa'}, }, { '@type': 'WebPage', '@id': 'https://my.site/about/#page', url: 'https://my.site/about/', name: "About | Alyssa P. Hacker's Website", inLanguage: 'en-US', isPartOf: { '@id': 'https://my.site/#site', }, about: {'@id': 'https://my.site/#alyssa'}, mainEntity: {'@id': 'https://my.site/#alyssa'}, }, ], }; ``` -------------------------------- ### Handle JSON-LD @graph with Interconnected Nodes Source: https://context7.com/google/schema-dts/llms.txt The Graph type provides a typed interface for JSON-LD documents with multiple top-level nodes under a '@graph' key. Each node can have an '@id' and be referenced by other nodes using an '@id' stub. ```typescript import type {Graph} from 'schema-dts'; const graph: Graph = { '@context': 'https://schema.org', '@graph': [ { '@type': 'Person', '@id': 'https://my.site/#alyssa', name: 'Alyssa P. Hacker', hasOccupation: { '@type': 'Occupation', name: 'LISP Hacker', qualifications: 'Knows LISP', }, mainEntityOfPage: {'@id': 'https://my.site/about/#page'}, subjectOf: {'@id': 'https://my.site/about/#page'}, }, { '@type': 'WebPage', '@id': 'https://my.site/about/#page', url: 'https://my.site/about/', name: "About | Alyssa P. Hacker's Website", inLanguage: 'en-US', about: {'@id': 'https://my.site/#alyssa'}, mainEntity: {'@id': 'https://my.site/#alyssa'}, }, { '@type': 'WebSite', '@id': 'https://my.site/#site', url: 'https://my.site', name: "Alyssa P. Hacker's Website", inLanguage: 'en-US', mainEntity: {'@id': 'https://my.site/#alyssa'}, }, ], }; // Type-checked: unknown property is caught at compile time const badGraph: Graph = { '@context': 'https://schema.org', '@graph': [ { '@type': 'Thing', // @ts-expect-error Unknown property unknownProp: 5, }, ], }; ``` -------------------------------- ### Add @context to Schema.org Types with WithContext Source: https://context7.com/google/schema-dts/llms.txt Use WithContext to add a required '@context' property to any Schema.org type T. The '@context' value must exactly match the URL used during typings generation. This is essential for embedding JSON-LD in HTML. ```typescript import type {Organization, Person, WithContext} from 'schema-dts'; // Person with context const person: WithContext = { '@context': 'https://schema.org', '@type': 'Person', name: 'Grace Hopper', birthDate: '1906-12-09', awards: [ 'Presidential Medal of Freedom', 'National Medal of Technology and Innovation', ], }; // Organization with context — '@context' must be exactly 'https://schema.org' const org: WithContext = { '@context': 'https://schema.org', '@type': 'Organization', name: 'Acme Corp', url: 'https://acme.com', logo: 'https://acme.com/logo.png', sameAs: ['https://twitter.com/acme', 'https://linkedin.com/company/acme'], }; // Type error: wrong context URL // @ts-expect-error const bad: WithContext = {'@context': 'https://google.com', '@type': 'Person'}; ``` ```typescript // Helper to serialize safely for injection into HTML function safeJsonLd(data: WithContext): string { return JSON.stringify(data) .replace(//g, '\u003E') .replace(/&/g, '\u0026') .replace(/'/g, '\u0027'); } // ``` -------------------------------- ### Safely Serialize JSON-LD for HTML Injection Source: https://github.com/google/schema-dts/blob/main/README.md A utility function to serialize JSON-LD data into a string suitable for embedding within an HTML ` ``` -------------------------------- ### MergeLeafTypes - Combine Schema.org Leaf Types Source: https://context7.com/google/schema-dts/llms.txt Use MergeLeafTypes to combine multiple concrete Schema.org leaf types into a single type. The '@type' property will be a tuple of string literals matching the input order, and properties will be the intersection of all leaf members. This is useful for JSON-LD nodes with multiple @type values. ```typescript import type { MergeLeafTypes, ProductLeaf, SoftwareApplicationLeaf, WithContext, } from 'schema-dts'; // Dual-typed product + software node const app: WithContext> = { '@context': 'https://schema.org', '@type': ['Product', 'SoftwareApplication'], // tuple order must match type order name: 'My App', offers: { '@type': 'Offer', price: 89, priceCurrency: 'USD', availability: 'https://schema.org/InStock', }, operatingSystem: 'Any', // from SoftwareApplication }; // Type error: tuple order swapped const bad: MergeLeafTypes<[ProductLeaf, SoftwareApplicationLeaf]>['@type'] = [ // @ts-expect-error Wrong order 'SoftwareApplication', // @ts-expect-error Wrong order 'Product', ]; // Type error: union alias used instead of concrete leaf type // @ts-expect-error Use ProductLeaf, not Product type Wrong = MergeLeafTypes<[import('schema-dts').Product, SoftwareApplicationLeaf]>; ``` -------------------------------- ### IdReference - Typed @id Stub for Cross-Referencing Source: https://context7.com/google/schema-dts/llms.txt IdReference is a type alias for { '@id': string }. It allows nodes to be defined once with an '@id' and referenced by IRI elsewhere in the document, useful for creating graphs with cross-references. ```typescript import type {Graph, IdReference} from 'schema-dts'; // IdReference used as a property value const ref: IdReference = {'@id': 'https://my.site/#alyssa'}; // Graph with cross-references const doc: Graph = { '@context': 'https://schema.org', '@graph': [ { '@type': 'Person', '@id': 'https://my.site/#alice', name: 'Alice', knows: {'@id': 'https://my.site/#bob'}, // IdReference }, { '@type': 'Person', '@id': 'https://my.site/#bob', name: 'Bob', knows: {'@id': 'https://my.site/#alice'}, // IdReference back }, ], }; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.