Skip to content

Commit 518c21d

Browse files
committed
types: introduce FieldWithPreferredType<T, U> type for readability of Field<T> type
1 parent 89bbfcc commit 518c21d

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

__tests__/toObject.spec-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe("toObject", () => {
5757
}>();
5858
});
5959

60-
test("form includes Field<T, U>", () => {
60+
test("form includes FieldWithPreferredType<T, U>", () => {
6161
type Foo = "A" | "B" | "C";
6262

6363
const form = defineForm({

docs/api/field.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ declare function field<T>(
1010
value: T | Ref<T>,
1111
schema?: FieldSchema,
1212
validateOptions?: ValidateOptions
13-
): Field<T, T>;
13+
): Field<T>;
1414
declare function field<T, U extends T>(
1515
value: T | Ref<T>,
1616
schema?: FieldSchema,
1717
validateOptions?: ValidateOptions
18-
): Field<T, U>;
18+
): FieldWithPreferredType<T, U>;
1919
```
2020

2121
### 1. `value` <Badge type="danger" text="Required" />

src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export type FormPropType<T extends () => Form> = PropType<
3232
ReturnType<T> & { $key?: number }
3333
>;
3434

35-
class Field<T, U extends T = T> {
35+
class Field<T> {
3636
private readonly $_valueRef: Ref<T>;
3737
private readonly $_errorRef: ComputedRef<yup.ValidationError | undefined>;
3838
public readonly $label: string;
@@ -88,6 +88,8 @@ class Field<T, U extends T = T> {
8888
}
8989
}
9090

91+
type FieldWithPreferredType<T, U extends T> = Field<T>;
92+
9193
class PrivateField<T> extends Field<T> {
9294
// Distinguish between Field and PrivateField in the structural type system
9395
private readonly $_isPrivateField!: true;
@@ -214,12 +216,12 @@ export function field<T>(
214216
value: T | Ref<T>,
215217
schema?: FieldSchema,
216218
validateOptions?: ValidateOptions
217-
): Field<T, T>;
219+
): Field<T>;
218220
export function field<T, U extends T>(
219221
value: T | Ref<T>,
220222
schema?: FieldSchema,
221223
validateOptions?: ValidateOptions
222-
): Field<T, U>;
224+
): FieldWithPreferredType<T, U>;
223225
export function field<T>(
224226
value: T | Ref<T>,
225227
schema?: FieldSchema,
@@ -278,8 +280,8 @@ type ToObjectOutput<T extends Form> = {
278280
? never
279281
: K]: T[K] extends Form
280282
? ToObjectOutput<T[K]>
281-
: T[K] extends Field<any, infer U>
282-
? U
283+
: T[K] extends FieldWithPreferredType<infer U1, infer U2>
284+
? U2
283285
: T[K] extends FormsField<infer U>
284286
? ToObjectOutput<ReturnType<U>>[]
285287
: never;

0 commit comments

Comments
 (0)