npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

json2ts-type

v1.1.0

Published

A CLI tool to convert JSON data into TypeScript type definitions. Supports Node.js, Browser, and VSCode extension environments.

Readme

get-json-type

English | 简体中文

一款将 JSON 数据转换为 TypeScript 类型定义的 CLI 工具。

功能

  • 支持多种数据源:URL、本地文件、剪贴板
  • 自动检测可选属性
  • 智能合并数组中的对象类型
  • 支持嵌套对象提取
  • 支持 Union 类型
  • 交互式命令行界面
  • 可作为 npm 包导入使用

安装

bun add json2ts-type

使用方式

CLI 命令行

从 URL 获取

# 基础用法
get-json-type url https://api.example.com/data

# 使用 Bearer 认证
get-json-type url https://api.example.com/data -b your-token

# 指定类型名称和输出文件
get-json-type url https://api.example.com/data -n ApiResponse -o types/api.ts

从文件读取

# 相对路径
get-json-type file ./data.json

# 绝对路径
get-json-type file /path/to/data.json -n MyType -o types/my-type.ts

从剪贴板读取

get-json-type clipboard -n ClipboardData -o types/clipboard.ts

交互式模式

# 直接运行命令,无需参数
get-json-type

# 或使用 bun dev
bun dev

作为库使用

import { parseJsonToType } from 'json2ts-type';

const jsonData = {
  id: 1,
  name: 'John',
  tags: ['admin', 'user'],
};

const result = parseJsonToType(jsonData, 'User');
console.log(result.code);

输出:

export interface User {
  id: number;
  name: string;
  tags: string[];
}

命令选项

url 命令

get-json-type url <source> [options]

| 选项 | 简写 | 说明 | | ------------------ | ---- | -------------------------------- | | --bearer <token> | -b | Bearer 认证令牌 | | --name <name> | -n | 生成的类型名称(默认:RootType) | | --output <file> | -o | 输出文件路径 |

file 命令

get-json-type file <source> [options]

| 选项 | 简写 | 说明 | | ----------------- | ---- | -------------------------------- | | --name <name> | -n | 生成的类型名称(默认:RootType) | | --output <file> | -o | 输出文件路径 |

clipboard 命令

get-json-type clipboard [options]

| 选项 | 简写 | 说明 | | ----------------- | ---- | -------------------------------- | | --name <name> | -n | 生成的类型名称(默认:RootType) | | --output <file> | -o | 输出文件路径 |

功能示例

可选属性检测

输入 JSON:

{
  "users": [
    { "id": 1, "name": "Alice", "email": "[email protected]" },
    { "id": 2, "name": "Bob" }
  ]
}

生成类型:

export interface User {
  id: number;
  name: string;
  email?: string;
}

export interface RootType {
  users: User[];
}

Union 类型支持

输入 JSON:

{
  "items": [1, "text", 42, "hello"]
}

生成类型:

export interface RootType {
  items: (number | string)[];
}

嵌套对象提取

输入 JSON:

{
  "user": {
    "profile": {
      "name": "John",
      "age": 30
    }
  }
}

生成类型:

export interface Profile {
  name: string;
  age: number;
}

export interface User {
  profile: Profile;
}

export interface RootType {
  user: User;
}