@formspec/core

中文文档
0.1.0-alpha.70发布于 2026/08/21MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 35.1万

Core utilities for formspec

原始使用文档
文档目录10 节
  1. Install
  2. Main Responsibilities
  3. Example
  4. Key Exports
  5. Form Types
  6. IR Types
  7. Extension API
  8. Utilities
  9. Entry Points
  10. License

@formspec/core

Shared types, canonical IR nodes, and extension registration APIs for FormSpec.

Install

pnpm add @formspec/core

Most app code should prefer formspec unless you are building tooling, extensions, or lower-level integrations.

Main Responsibilities

  • Form element and state types
  • Canonical IR types used by the build pipeline
  • Constraint-definition metadata
  • Extension registration helpers for custom types, constraints, and annotations
  • Shared type guards

Example

import type { FormElement, FormIR } from "@formspec/core";
import {
  defineAnnotation,
  defineConstraint,
  defineCustomType,
  defineExtension,
} from "@formspec/core";

function isFieldElement(element: FormElement): boolean {
  return element._type === "field";
}

const decimalType = defineCustomType({
  typeName: "Decimal",
  tsTypeNames: ["Decimal"],
  toJsonSchema: (_payload, vendorPrefix) => ({
    type: "string",
    [`${vendorPrefix}-decimal`]: true,
  }),
});

const displayCurrency = defineAnnotation({
  annotationName: "DisplayCurrency",
  inheritFromBase: "local-wins",
});

const extension = defineExtension({
  extensionId: "x-example/decimal",
  types: [decimalType],
  constraints: [],
  annotations: [displayCurrency],
});

inheritFromBase is semantic inheritance only. It lets a type-level custom annotation flow through class/interface heritage and named type-alias entry points when the derived declaration does not declare the same extension annotation locally, but it does not serialize the annotation to JSON Schema. Schema emission remains controlled by the optional existing toJsonSchema hook, so property-level annotation inheritance and metadata-slot inheritance remain out of scope.

Key Exports

Form Types

  • FormSpec
  • FormElement
  • AnyField
  • Group
  • Conditional
  • FieldState
  • FormState

IR Types

  • FormIR
  • FieldNode
  • LayoutNode
  • TypeNode
  • ConstraintNode
  • AnnotationNode
  • Provenance

Extension API

  • defineExtension
  • defineConstraint
  • defineConstraintTag
  • defineAnnotation
  • defineCustomType

Utilities

  • normalizeConstraintTagName
  • createInitialFieldState
  • IR_VERSION
  • field and layout type guards

Entry Points

Entry pointPurpose
@formspec/coreStable public types and extension APIs
@formspec/core/internalsUnstable low-level internal types

License

This package is part of the FormSpec monorepo and is released under the MIT License. See LICENSE for details.

版本与发布记录

@formspec/core 共发布 29 个版本,首次发布于 2026/01/29;过去 12 个月发布了 29 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.0-alpha.70latest2026/08/212 个月前
0.1.0-alpha.632026/04/246 个月前
0.1.0-alpha.592026/04/226 个月前
0.1.0-alpha.552026/04/206 个月前
0.1.0-alpha.542026/04/206 个月前
0.1.0-alpha.532026/04/206 个月前

其他发布通道:alpha → 0.1.0-alpha.0。

关于 @formspec/core 的常见问题

@formspec/core 是什么?

Core utilities for formspec

如何安装 @formspec/core?

在项目目录运行 npm install @formspec/core,或使用 pnpm add / yarn add / bun add。

@formspec/core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@formspec/core 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@formspec/core 可以商用吗?

@formspec/core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@formspec/core 的下载量有多少?

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

@formspec/core 还在维护吗?

最新版本 0.1.0-alpha.70 发布于 2026/08/21(2 个月前);至今共发布 29 个版本,其中过去 12 个月发布了 29 个;首次发布于 2026/01/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。