Skip to content

Commit d8a6647

Browse files
committed
feat: add a couple type helpers
1 parent f8276d1 commit d8a6647

File tree

6 files changed

+24
-2
lines changed

6 files changed

+24
-2
lines changed

TODO.md

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
- Consider adding a `beforeSubmit` callback
2-
31
// follows
42

53
- Globally setting some settings with a context or something.

packages/core/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export * from "./createValidator";
1010
export * from "./native-form-data/flatten";
1111
export * from "./constants";
1212
export * from "./input-types";
13+
export * from "./type-helpers";
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { FormScope } from "../dist";
2+
import { FieldValue } from "./type-helpers";
3+
4+
describe("FieldValue", () => {
5+
it("should work with a single field", () => {
6+
type TestForm = FormScope<{ foo: string }>;
7+
type Foo = FieldValue<TestForm, "foo">;
8+
expectTypeOf<Foo>().toEqualTypeOf<string>();
9+
});
10+
});

packages/core/src/type-helpers.tsx

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { StringToPathTuple, ValidStringPaths, ValueAtPath } from "@rvf/set-get";
2+
import { FormScope } from "./form";
3+
4+
export type ScopedValues<Scope> = Scope extends FormScope<infer U> ? U : never;
5+
6+
export type FieldValue<
7+
Scope,
8+
Path extends ValidStringPaths<ScopedValues<Scope>>,
9+
> = ValueAtPath<ScopedValues<Scope>, StringToPathTuple<Path>>;

packages/react/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export {
2323
type NumberInputValue,
2424
type NativeValueByType,
2525
type ValueOfInputType,
26+
type ScopedValues,
27+
type FieldValue,
2628
} from "@rvf/core";
2729
export { type FormApi, type FormFields } from "./base";
2830
export { useForm, FormOpts } from "./useForm";

packages/remix/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export {
4848
type GetInputProps,
4949
type GetInputPropsParam,
5050
type MinimalInputProps,
51+
type ScopedValues,
52+
type FieldValue,
5153
} from "@rvf/react";
5254
export { useForm, type RemixFormOpts as FormScopeRemixOpts } from "./useForm";
5355
export { ValidatedForm, type ValidatedFormProps } from "./ValidatedForm";

0 commit comments

Comments
 (0)