@dazl/typescript-schema-extract

中文文档
15.5.2发布于 2026/07/09UNLICENSEDES Module近 30 天下载 1万

Schema extract transforms all the types (explicit or inferred) in your code files into JSON-Schema schemas. Schema extraction is a necessary step to create automated documentation for your code.

原始使用文档
文档目录5 节
  1. Usage
  2. Result example
  3. Linker
  4. TS Transformer
  5. Creating a custom linker

Schema Extract

Schema extract transforms all the types (explicit or inferred) in your code files into JSON-Schema schemas. Schema extraction is a necessary step to create automated documentation for your code.

It is composed of two parts, the ts transformer and linker.

Schema service

the schema service allows watching specific schema's, handling the caching between requests. it sends an update event specificing which types have been updated after files are invalidated

it allows watching schemas for modules / ast nodes. fetching a limited Schema depth and expanding a specific branch of the schema later.

this allows the GUI to only request the parts of the schema it reads.

always remember to release your watched paths

Usage

To use the Schema Extract project to extract type data from your files, import the createLinker function from @dazl/typescript-schema-extract. This function receives a string array of file paths and returns an initialized linker class.

After creating a linker, invoke linker.flatten. It receives two arguments and will return a linked schema of the entity we requested (in exportName):

NameTypeDescription
filestringThe path to the file we want to transform
exportNamestringThe exported entity in that file, that we want to link
    import {createLinker} from '@dazl/typescript-schema-extract';
    const files = [
        'myProject/src/file-a.ts',
        'myProject/src/file-b.ts'
    ];
    const linker = createLinker(files);
    const linkedSchema = linker.flatten(files[0], 'IAnimal');

Note: If you want to use the linker with a different extractor please see the creating a custom linker section.
Also, at the moment it is not possible to automatically link all the exports of a given file. You can do this by going over the exports of the file and invoking flatten for each one, but this may not be an efficient process.

Result example

To better understand how the transformation works, take a look at the following interface:

// file-a.ts
export interface IAnimal {
  name: string;
  hasTail: boolean;
  makeNoise: (sound: ISound) => void;
  isTamed?: 'YES' | 'NO';
}

If we transform it using the TS transformer we will get the following schema:

{
  "$id": "/index.tsx",
  "$ref": "common/module",
  "properties": {},
  "definitions": {
    "IAnimal": {
      "properties": {
        "name": {
          "type": "string"
        },
        "hasTail": {
          "type": "boolean"
        },
        "makeNoise": {
          "$ref": "common/function",
          "arguments": [
            {
              "$ref": "#ISound",
              "name": "sound"
            }
          ],
          "returns": {
            "$ref": "common/undefined"
          },
          "requiredArguments": ["sound"]
        },
        "isTamed": {
          "type": "string",
          "enum": ["YES", "NO"]
        }
      },
      "required": ["name", "hasTail", "makeNoise"],
      "$ref": "common/interface"
    }
  }
}

Linker

The linker receives a file name and the name of an export in that file and returns the linked schema of that export.

How is it any different than the ts transformer? When we transform a schema using the ts transformer, the result we get will only reference other types (import or from the same file) using the $ref property.

The linker flattens some of these types by linking them together.

Example

Let's look at InterfaceB in the following code:

export interface InterfaceA<T> {
  something: T;
}
export interface InterfaceB extends InterfaceA<string> {
  somethingElse: number;
}

If we use the TS transformer to transform this file we will get the following schema:

{
  "$id": "/index.tsx",
  "$ref": "common/module",
  "properties": {},
  "definitions": {
    "InterfaceA": {
      "$ref": "common/interface",
      "properties": {
        "something": { "$ref": "#InterfaceA!T" }
      },
      "required": ["something"],
      "genericParams": [{ "name": "T" }]
    },
    "InterfaceB": {
      "$ref": "common/interface",
      "properties": {
        "somethingElse": { "type": "number" }
      },
      "required": ["somethingElse"],
      "genericArguments": [{ "type": "string" }],
      "extends": {
        "$ref": "#InterfaceA"
      }
    }
  }
}

But we are missing some crucial information about InterfaceB. After linking this is how the schema for InterfaceB will look like:

    "InterfaceB": {
        $ref: "common/interface",
        properties: {
            something: {
                inheritedFrom: '#InterfaceA',
                type: 'string'
            },
            somethingElse: {
                type: 'number'
            }
        },
        required: ['somethingElse', 'something']
    };

Linking rules

In order to avoid running into infinite loops, the linker does not link every member of every schema. It does link:

  • extends - Classes and interfaces that use the extends keyword. (however the linker will not link members of interfaces)
  • Generic types
  • Intersection types - We may need the linked schemas to intersect two or more different types.

What won't be linked?

  • Imports - If you import a type from a different file, it will be represented as a reference: $ref: "myProject/src/util#myFunction"
  • References to other types - Any form of referencing a different type or interface like type A = B or interface InterfaceA { type: InterfaceB }. This also includes function using other types as arguments or return values.

TS Transformer

Transforms a typescript source file into a non linked JSON-Schema. By default, the linker is using this transformer and unless you want a non linked schema, there is no need to use this transformer.

Usage

Import the transform function from @dazl/typescript-schema-extract. This function receives the following five arguments:

NameTypeDescription
checkertypescript.TypeCheckerA typescript checker connected to the desired files
sourceFiletypescript.SourceFileThe typescript source of that we want to transform
modulePathstringThe path to the module base directory
projectPathstringThe name of the project the files are in (will be removed in the future)
pathUtilIFileSystemPathA path utility to be used to access directories and file. (you can use path.posix)

For example if we want to transform our own code:

    import path from 'path';
    import compilerOptions from './utils';

    const projectPath = '/Projects/MySecretProject/;
    const fileName = projectPath + 'secret-stuff.ts'
    // TS setup
    const program = typescript.createProgram([fileName], compilerOptions);
    const sourceFile = program.getSourceFile(fileName);

    const schema = transform(program.getTypeChecker(), sourceFile, fileName, 'MySecretProject', path.posix);

Schema structure & types

You can find the structure of schemas and the different types we support in json-schema-types

Creating a custom linker

Note: At the moment we don't export the SchemaLinker class. We will either export it or add a function that receives an extractor and returns a linker

Using createLinker creates a typescript based linker, but the linker is not limited only to typescript. When creating a new linker class, the linker receives an extractor object:

    export interface ILinkerOptions {
        getSchema: (path: string) => ModuleSchema;
    }

ILinkerOptions receives a path to a file and returns the JSON-Schema created from that file.

To use a different extractor with the linker, you just need to create a new linker class with the extractor and invoke flatten.

    flatten(filePath: string, typeName: string): Schema

The flatten function receives the path to file and the type inside of it we want to link.

import { SchemaLinker } from '@dazl/typescript-schema-extract/?????';
import { myExtractor } from './secretStuff';

function linkSchema(filePath, typeName) {
  const customLinker = new SchemaLinker(myExtractor);
  return linker.flatten(filePath, typeName);
}

版本与发布记录

@dazl/typescript-schema-extract 共发布 10 个版本,首次发布于 2025/08/28;过去 12 个月发布了 9 个版本。

版本发布日期距今
15.5.2latest2026/07/094 个月前
15.5.12026/07/094 个月前
15.5.02026/05/106 个月前
15.4.12026/05/076 个月前
15.4.02026/04/067 个月前
15.3.52025/12/1510 个月前

关于 @dazl/typescript-schema-extract 的常见问题

@dazl/typescript-schema-extract 是什么?

Schema extract transforms all the types (explicit or inferred) in your code files into JSON-Schema schemas. Schema extraction is a necessary step to create automated documentation for your code.

如何安装 @dazl/typescript-schema-extract?

在项目目录运行 npm install @dazl/typescript-schema-extract,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:typescript(^4.0.0 || ^5.0.0 || ^6.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@dazl/typescript-schema-extract 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/dazl__typescript-schema-extract 是否存在。

@dazl/typescript-schema-extract 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@dazl/typescript-schema-extract 可以商用吗?

@dazl/typescript-schema-extract 使用 UNLICENSED 许可。作者声明为 UNLICENSED,即不授予公开使用许可(与公共领域的 Unlicense 不同)。使用前需获得作者授权。以上为概括,不构成法律意见,请以许可原文为准。

@dazl/typescript-schema-extract 的下载量有多少?

根据 npm 官方下载统计,@dazl/typescript-schema-extract 在 2026/10/12 – 2026/11/10 这 30 天内被下载 10,090 次,日均约 336 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dazl/typescript-schema-extract 还在维护吗?

最新版本 15.5.2 发布于 2026/07/09(4 个月前);至今共发布 10 个版本,其中过去 12 个月发布了 9 个;首次发布于 2025/08/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。