Skip to content

Commit 99eb6e2

Browse files
committed
feat: add InferDataPack, InferDataUnpack helpers
1 parent da8513e commit 99eb6e2

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@gramio/callback-data",
3-
"version": "0.0.8",
3+
"version": "0.0.9",
44
"main": "dist/index.js",
55
"devDependencies": {
66
"@biomejs/biome": "^1.9.4",

src/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,16 @@ export class CallbackData<
316316
return this as any;
317317
}
318318
}
319+
320+
export type InferDataPack<T extends CallbackData> = T extends CallbackData<
321+
infer SchemaType,
322+
infer SchemaTypeInput
323+
>
324+
? SchemaTypeInput
325+
: never;
326+
export type InferDataUnpack<T extends CallbackData> = T extends CallbackData<
327+
infer SchemaType,
328+
infer SchemaTypeInput
329+
>
330+
? SchemaType
331+
: never;

tests/types/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { expectTypeOf } from "expect-type";
2-
import { CallbackData } from "../../src/index.ts";
2+
import {
3+
CallbackData,
4+
type InferDataPack,
5+
type InferDataUnpack,
6+
} from "../../src/index.ts";
37

48
const callbackDataString = new CallbackData("test").string("name", {
59
optional: true,
@@ -118,3 +122,10 @@ const defaultUnpackedDataWithDefaults = defaultCallbackData.unpack(
118122
expectTypeOf(defaultUnpackedDataWithDefaults).toEqualTypeOf<{
119123
isActive: boolean;
120124
}>();
125+
126+
{
127+
const cd = new CallbackData("test").boolean("test", { default: true });
128+
129+
expectTypeOf<InferDataPack<typeof cd>>().toEqualTypeOf<{ test?: boolean }>();
130+
expectTypeOf<InferDataUnpack<typeof cd>>().toEqualTypeOf<{ test: boolean }>();
131+
}

0 commit comments

Comments
 (0)