schema-dts

googleApache-2.0活跃维护

为 Schema.org 词汇表生成 JSON-LD 的 TypeScript 类型定义,让结构化数据在 TS 项目里获得类型检查和自动补全,避免手写字段出错。

JSON-LD TypeScript types for Schema.org vocabulary

v2.1.0发布于 2026-10-02 19:11 UTC1,260star +2 / 7 天59fork同步于 2026-11-10 11:49 UTC

怎么上手

由 README 提炼,执行前请确认
npm install --save-dev schema-dts
硬件要求按 README 说明

README 未说明,纯 TypeScript 类型包,无 GPU 或特殊硬件要求

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

给 Schema.org 的 JSON-LD 提供 TypeScript 类型定义,让结构化数据写错就在编译期报错

为 Schema.org 词汇表生成 JSON-LD 的 TypeScript 类型定义,让结构化数据在 TS 项目里获得类型检查和自动补全,避免手写字段出错。

核心能力

  • 用判别式联合类型覆盖 Schema.org 词表,提供补全与严格校验
  • 导出 WithContext 给顶层对象补上 @context 属性
  • 提供 Graph 类型与 @id 引用桩,支持多节点互联的 JSON-LD 图
  • 导出 *Leaf 具体接口,可在 class 中 implements 单一 @type 形状
  • 附带 schema-dts-gen 命令行,按指定本体版本与层级生成类型

容易踩的坑

schema-dts 只含最新版核心层类型,不含 pending 等非核心层,需要时得用 schema-dts-gen 自行生成;MergeLeafTypes 只接受 ProductLeaf 这类具体 leaf 类型,传联合别名 Product 会报错;把 JSON-LD 注入 script 标签前要转义 < > & ' 以防 XSS

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v2.1.0
  1. v2.1.0最新正式版2026-10-02 · 2026-10-02 19:11 UTC

    Changes in schema-dts v2.1.0

    Changes in schema-dts-gen v2.0.1

    • Address security issue if using the generator directly with untrusted ontologies

    New Contributors

    Full Changelog: https://github.com/google/schema-dts/compare/v2.0.0...v2.1.0

  2. v2.0.0schema-dts & schema-dts-gen v2.0.02026-03-23 · 2026-03-23 22:18 UTC
    查看更新说明

    Changes in schema-dts

    • Supports the latest Schema.org release from Schema.org v30 -- See https://schema.org/docs/releases.html#v30.0

    • Input and Output constraints are now supported, for example:

      import type {SearchAction, WebSite, WithActionConstraints} from 'schema-dts';
      
      const potentialAction: WithActionConstraints<SearchAction> = {
        '@type': 'SearchAction',
        'query-input': 'required name=search_term_string',
        // ...
      };
      
      const website: WebSite = {
        '@type': 'WebSite',
        potentialAction: {
          '@type': 'SearchAction',
          'query-input': 'required name=search_term_string',
        } as WithActionConstraints<SearchAction>,
      };
      
    • Breaking change: Update typings for Roles so that they are not recursive, see #205 for more details

    • Breaking change: Quantity is now a core DataType, this is a change done in schema.org v30.0 and will technically make certain formerly-legal (but likely invalid) assignments no longer work

    • "Leaf" types are now exported. While objects like Thing or Organization require a type union that includes the object itself, it can also include any of its children. Now, if you want type something as a Thing exactly, you can import ThingLeaf which can only be a Thing and does not allow sub-types.

    • Support MergeLeafTypes, which fixes a long-running user request (#179 #189 #203), allowing a multi-typed schema object to be declared. Shout out to @mjy9088 and @cochinescu for the work to get this working. This allows declarations like this to be made:

      import type { MergeLeafTypes,ProductLeaf, SoftwareApplicationLeaf,  WithContext } from 'schema-dts';
      
      const app: WithContext<MergeLeafTypes<[ProductLeaf, SoftwareApplicationLeaf]>> =
        {
          '@context': 'https://schema.org',
          '@type': ['Product', 'SoftwareApplication'],
          name: 'My App',
          offers: {
            '@type': 'Offer',
            price: 89,
            priceCurrency: 'USD',
          },
          operatingSystem: 'Any',
        };
      

      while #189 and #179 are not completely addressed since a random Thing cannot be a merged type, this allows some multi-type objects to exist, though they need to be explicitly declared as such by the developer.

    • Technically a breaking change: Some non-schema.org types exported in schema-dts are now renamed. These are included mostly for equivalence because the Schema.org ontology defines them, but most users should not depend on them. The names for these classes are now escaped fully-qualified IRIs instead of just the "in-context" name. This is needed because new types with the same "in-context" name have been added as external references, e.g. www.omg.org/spec/Commons/DatesAndTimes/Date which conflicts with schema.org/Date. Now, this is exported as www_omg_org_spec_Commons_DatesAndTimes_Date.

    Changes in schema-dts-gen

    • Compatibility with Schema.org v30
    • Breaking change: schema.org/Quantity is now emitted as a DataType
    • Breaking change: out-of-context classes are now exported with an escaped fully-qualified IRI instead of just the hypothetical 'in-context' name. For example: www.omg.org/spec/Commons/DatesAndTimes/Date renders as Date if "@context": "www.omg.org/spec/Commons/DatesAndTimes/" only. Otherwise, it renders as namespace_Date if "@context" { "namespace": "www.omg.org/spec/Commons/DatesAndTimes/" }, and www_omg_org_spec_Commons_DatesAndTimes_Date if no context entry exists
    • Breaking change: the emitted output now depends on https://www.npmjs.com/package/schema-dts-lib -- the first two lines from the emitted output will include import type {...} from 'schema-dts-lib'; followed by export type { ... };.

    What's Changed

  3. v1.1.5schema-dts & schema-dts-gen v1.1.52025-03-01 · 2025-03-01 21:05 UTC
    查看更新说明

    Changes in schema-dts

    • Includes the latest schema from Schema.org v28
    • TypeScript is no longer a peer-dependency of schema-dts, this helps users that install schema-dts as a regular "dependency" (instead of "devDependency")

    Diff from v1.1.2: https://www.diffchecker.com/JDk6fuVP/

    Changes in schema-dts-gen

    • Updates dependencies
  4. v1.1.2schema-dts & schema-dts-gen v1.1.22023-02-24 · 2023-02-24 18:42 UTC
    查看更新说明

    Changes in schema-dts

    • Includes the latest schema from Schema.org v15
    • Switch back to include the "all layers" schema (including the "Pending" layer) (this was the case in earlier versions and regressed accidentally in the last few releases)
    • Clearer TypeScript outputs, substituting unnecessary declare type with simply type

    Diff from v1.1.0: https://www.diffchecker.com/bB0J9Iie/

    Changes in schema-dts-gen

    • Update to the latest TypeScript APIs
    • Switch default ontology from "current" layer to "all" layers
    • Updates to various dependencies
  5. v1.1.0schema-dts & schema-dts-gen v1.1.02022-02-24 · 2022-02-24 22:00 UTC
    查看更新说明

    Small, backwards-compatible improvements to schema-dts and schema-dts-gen

    Changes in schema-dts

    • Improved comment quality by properly including unicode and unescaped characters. (thanks to #174)
    • Includes latest Schema.org additions of Healthcare Businesses

    Diff from 1.0.0: https://www.diffchecker.com/RtKuuqMG

    Changes in schema-dts-gen

    • Uses N3.js to parse N-Triple files instead of a custom regex, this has a number of benefits:
      • --ontology can now be an .nt file or a .tt Turtle file
      • Improved output comment quality by properly including unicode and unescaped characters
      • Can parse string literals of length 0 or 1 (previously the shortest string that could be parsed in input was 2 characters long)
    • Can use rdfs:domain and rdfs:range instead of schema:domainIncludes and schema:rangeIncludes -- See #172
    • Parser does not choke when seeing OWL properties (though it doesn't explicitly support them) - See #172
    • Accept "unnamed" IRIs, such as "https://schema.org" (as opposed to "https://schema.org/Thing"), and handle them properly
    • Less special-handling of meta-schema (isPartOf, meta comments, etc.) -- The converter is more comfortable with entities it doesn't know about

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    Build Status Coverage Status schema-dts npm version schema-dts-gen version

    schema-dts

    JSON-LD TypeScript types for Schema.org vocabulary.

    schema-dts provides TypeScript definitions for Schema.org vocabulary in JSON-LD format. The typings are exposed as complete sets of discriminated type unions, allowing for easy completions and stricter validation.

    Example of Code Completion using schema-dts

    This repository contains two NPM packages:

    • schema-dts-gen Providing a command-line tool to generate TypeScript files based on a specific Schema version and layer.
    • schema-dts Pre-packaged TypeScript typings of latest Schema.org schema, without pending and other non-core layers.

    Note: This is not an officially supported Google product.

    Usage

    To use the typings for your project, simply add the schema-dts NPM package to your project:

    npm install --save-dev schema-dts
    

    Then you can use it by importing "schema-dts".

    Root context

    You will usually want your top-level item to include a @context, like https://schema.org. In order for your object type to accept this property, you can augment it with WithContext, e.g.:

    import type {Person, WithContext} from 'schema-dts';
    
    const p: WithContext<Person> = {
      '@context': 'https://schema.org',
      '@type': 'Person',
      name: 'Eve',
      affiliation: {
        '@type': 'School',
        name: 'Nice School',
      },
    };
    

    Merging multiple concrete types

    Some Schema.org objects can legitimately carry multiple concrete @type values. For those advanced cases, schema-dts exports leaf types alongside the usual union aliases, plus a MergeLeafTypes helper for combining them:

    import type {
      MergeLeafTypes,
      ProductLeaf,
      SoftwareApplicationLeaf,
      WithContext,
    } from 'schema-dts';
    
    const product: WithContext<
      MergeLeafTypes<[ProductLeaf, SoftwareApplicationLeaf]>
    > = {
      '@context': 'https://schema.org',
      '@type': ['Product', 'SoftwareApplication'],
      name: 'My App',
      offers: {
        '@type': 'Offer',
        price: 89,
        priceCurrency: 'USD',
      },
      operatingSystem: 'Any',
    };
    

    MergeLeafTypes expects concrete leaf types such as ProductLeaf, not union aliases such as Product.

    Implementing schema types in a class

    Every Schema.org type is exported twice: as a union alias (e.g. Person, which also spans its subtypes plus an id-reference string) and as a concrete "leaf" interface (e.g. PersonLeaf) describing exactly one @type. Because the leaf interface is a plain object type, you can implements it from a class, which the union alias does not allow:

    import type {PersonLeaf} from 'schema-dts';
    
    class Employee implements PersonLeaf {
      readonly '@type' = 'Person';
      name: string;
    
      constructor(name: string) {
        this.name = name;
      }
    }
    

    Reach for the leaf interface (PersonLeaf) instead of the union alias (Person) whenever you need a single, concrete object shape to implement or extend.

    Graphs and IDs

    JSON-LD supports '@graph' objects that have richer interconnected links between the nodes. You can do that easily in schema-dts by using the Graph type.

    Notice that any node can have an @id when defining it. And you can reference the same node from different places by simply using an ID stub, for example { '@id': 'https://my.site/about/#page } below is an ID stub.

    The example below shows potential JSON-LD for an About page. It includes definitions of Alyssa P. Hacker (the author & subject of the page), the specific page in this URL, and the website it belongs to. Some objects are still defined as inline nested objects (e.g. Occupation), since they are only referenced by their parent. Other objects are defined at the top-level with an @id, because multiple nodes refer to them.

    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'},
        },
      ],
    };
    

    Input and Output constraints

    Actions support additional information to describe inputs and outputs, so each property can be annotated using -input and -output suffixes and you can do that in schema-dts by using the WithActionConstraints type for each action type you want to annotate. See the following examples:

    import type {SearchAction, WithActionConstraints} from 'schema-dts';
    
    const potentialAction: WithActionConstraints<SearchAction> = {
      '@type': 'SearchAction',
      'query-input': 'required name=search_term_string',
      // ...
    };
    
    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<SearchAction>,
    };
    

    Real-world examples

    Below are common patterns for using schema-dts types when building JSON-LD for real websites.

    Organization and WebSite (every site should have these)
    import type {Organization, WebSite, WithContext} from 'schema-dts';
    
    const org: WithContext<Organization> = {
      '@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'],
    };
    
    const site: WithContext<WebSite> = {
      '@context': 'https://schema.org',
      '@type': 'WebSite',
      name: 'Acme Corp',
      url: 'https://acme.com',
    };
    
    Product with Offer (e-commerce)
    import type {Product, WithContext} from 'schema-dts';
    
    const product: WithContext<Product> = {
      '@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',
      },
    };
    
    FAQPage (content sites)
    import type {FAQPage, WithContext} from 'schema-dts';
    
    const faq: WithContext<FAQPage> = {
      '@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.',
          },
        },
      ],
    };
    
    Article (blog posts)
    import type {Article, WithContext} from 'schema-dts';
    
    const article: WithContext<Article> = {
      '@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.',
    };
    
    Injecting JSON-LD into HTML

    When serializing JSON-LD for injection into a <script> tag, escape characters that could break out of the tag or enable XSS:

    function safeJsonLd(data: WithContext<Thing>): string {
      return JSON.stringify(data)
        .replace(/</g, '\\u003C')
        .replace(/>/g, '\\u003E')
        .replace(/&/g, '\\u0026')
        .replace(/'/g, '\\u0027');
    }
    
    // Use in HTML:
    // <script type="application/ld+json">${safeJsonLd(product)}</script>
    

    Tip: See examples.md for more integration patterns, including React, Next.js, Astro, and other frameworks.

    Schema Typings Generator

    The Schema Typings Generator is available in the schema-dts-gen package.

    npm install --save-dev schema-dts-gen
    npx schema-dts-gen --ontology=https://schema.org/version/latest/schemaorg-all-https.nt
    

    Command line usage:

    • Specify your ontology

      • Specify --ontology: An HTTPs URL to an .nt NTriple file declaring your ontology.

        Must be compatible with Schema.org, including the Schema.org DataTypes and specifying a top-level Thing type.

    • --context: Defaults to https://schema.org, the value or values to be used with the "@context" property.

      Can be either a single URL, or a comma separated list of two or more name:URL pairs.

      The context affects names of string properties in types, as well as the values of an object's "@type".

    • --deprecated/--nodeprecated: Whether or not to include deprecated Schema.org types and properties. When included, these types will still be marked with @deprecated JSDOC tags.

    • --verbose: Outputs additional logs and debugging notes to stderr.

    Developers

    Use NPM to install dependencies:

    npm install
    

    We have wrappers around tsc and tsc --build to build our generator other .d.ts files.

    To generate TypeScript from the latest Schema.org Schema:

    npm run build-gen && npm run build-schema
    

    or simply build the schema-dts generator:

    npm run build-gen
    

    To contribute changes, see the CONTRIBUTING.md file.