Files
query/demo/type-inference-demo.ts
abearxiong ecb69ba326 refactor: migrate from Rollup to Bun for build configuration
feat: update adapter to use globalThis for origin resolution

fix: remove unused ClientQuery export from query.ts

chore: update tsconfig to include test files and set rootDir

feat: add create-query functionality for dynamic API generation

feat: implement QueryApi with enhanced type inference from JSON Schema

test: add comprehensive API tests for QueryApi functionality

test: create demo routes and schemas for testing purposes

docs: add type inference demo for QueryApi usage
2026-02-17 21:39:41 +08:00

68 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { QueryApi } from "../src/query-api";
const api = new QueryApi();
// 示例 1: args 的 value 是 string 类型
api.post(
{
path: "/users",
metadata: {
args: {
name: "John",
email: "john@example.com"
}
}
},
{
pos: "someValue", // ✅ TypeScript 会推断 pos 应该是 string 类型
other: "data"
}
);
// 示例 2: args 的 value 是 number 类型
api.post(
{
path: "/products",
metadata: {
args: {
id: 123,
price: 99.99
}
}
},
{
pos: 456, // ✅ TypeScript 会推断 pos 应该是 number 类型
other: "data"
}
);
// 示例 3: args 的 value 是混合类型
api.post(
{
path: "/orders",
metadata: {
args: {
orderId: 123,
status: "pending",
total: 99.99
}
}
},
{
pos: "status", // ✅ TypeScript 会推断 pos 可以是 string 或 number
// pos: 999, // 这也是合法的
other: "data"
}
);
// 示例 4: 没有 metadata 或 args
api.post(
{
path: "/test"
},
{
pos: undefined, // ✅ pos 类型为 never只能是 undefined
other: "data"
}
);