百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
U

untypeable

> 编程语言
开源

通过零束包大小选项,安全地访问任何 API。

383 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

通过零束包大小选项,安全地访问任何 API。

Untypeable

Get type-safe access to any API, with a zero-bundle size option.

The Problem

If you're lucky enough to use tRPC, GraphQL, or OpenAPI, you'll be able to get type-safe access to your API - either through a type-safe RPC or codegen.

But what about the rest of us?

What do you do if your API has no types?

Solution

Enter untypeable - a first-class library for typing API's you don't control.

  • Get autocomplete on your entire API, without needing to set up a single generic function.
  • Simple to configure, and extremely flexible.
  • Choose between two modes:
  • Zero bundle-size: use import type to ensure untypeable adds nothing to your bundle.
  • Strong types: integrates with libraries like Zod to add runtime safety to the types.
  • ✨ Keep things organized with helpers for merging and combining your config.
  • ❤️ You bring the fetcher, we bring the types. There's no hidden magic.

Quickstart

npm i untypeable

…

SWAPI Example

We've added a full example of typing swapi.dev.

Zero-bundle mode

You can set up untypeable to run in zero-bundle mode. This is great for situations where you trust the API you're calling, but it just doesn't have types.

To set up zero-bundle mode, you'll need to:

  1. Define your router in a file called router.ts.
  2. Export the type of your router: export type MyRouter = typeof router;
typescript
// router.ts

import { initUntypeable } from "untypeable";

const u = initUntypeable();

type User = {
  id: string;
  name: string;
};

const router = u.router({
  "/user": u.input().output(),
});

export type MyRouter = typeof router;
  1. In a file called client.ts, import createTypeLevelClient from untypeable/type-level-client.
typescript
// client.ts

import { createTypeLevelClient } from "untypeable/client";
import type { MyRouter } from "./router";

export const client = createTypeLevelClient(() => {
  // your implementation...
});

How does this work?

This works because createTypeLevelClient is just an identity function, which directly returns the function you pass it. Most modern bundlers are smart enough to collapse identity functions and erase type imports, so you end up with:

typescript
// client.ts

export const client = () => {
  // your implementation...
};

Runtime-safe mode

Sometimes, you just don't trust the API you're calling. In those situations, you'll often like to validate the data you get back.

untypeable offers first-class integration with Zod. You can pass a Zod schema to u.input and u.output to ensure that these values are validated with Zod.

typescript
import { initUntypeable, createSafeClient } from "untypeable";
import { z } from "zod";

const u = initUntypeable();

const router = u.router({
  "/user": u
    .input(
      z.object({
        id: z.string(),
      }),
    )
    .output(
      z.object({
        id: z.string(),
        name: z.string(),
      }),
    ),
});

export const client = createSafeClient(router, () => {
  // Implementation...
});

Now, every call made to client will have its input and output verified by the zod schemas passed.

Configuration & Arguments

untypeable lets you be extremely flexible with the shape of your router.

Each level of the router corresponds to an argument that'll be passed to your client.

typescript
// A router that looks like this:
const router = u.router({
  github: {
    "/repos": {
      GET: u.output(),
      POST: u.output(),
    },
  },
});

const client = createTypeLevelClient(() => {});

// Will need to be called like this:
client("github", "/repos", "POST");

You can set up this argument structure using the methods below:

.pushArg

Using the .pushArg method when we initUntypeable lets us add new arguments that must be passed to our client.

…

You can call this as many times as you want!

typescript
const u = initUntypeable()
  .pushArg()
  .pushArg();

const router = u.router({
  "/": {
    GET: {
      foo: u.output,
    },
  },
});

.unshiftArg

You can also add an argument at the start using .unshiftArg. This is useful for when you want to add different base endpoints:

typescript
const u = initUntypeable().unshiftArg();

const router = u.router({
  github: {
    "/repos": u.output(),
  },
});

.args

Useful for when you want to set the args up manually:

typescript
const u = initUntypeable().args();

const router = u.router({
  "any-string": {
    "any-other-string": {
      "yet-another-string": u.output(),
    },
  },
});

Organizing your routers

.add

You can add more detail to a router, or split it over multiple calls, by using router.add.

typescript
const router = u
  .router({
    "/": u.output(),
  })
  .add({
    "/user": u.output(),
  });

.merge

You can merge two routers together using router.merge. This is useful for when you want to combine multiple routers (perhaps in different modules) together.

typescript
import { userRouter } from "./userRouter";
import { postRouter } from "./postRouter";

export const baseRouter = userRouter.merge(postRouter);

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言