From 0af8b0048606a6adc1c07c3dbf13e3e3066caab1 Mon Sep 17 00:00:00 2001 From: "Beier (Bill)" Date: Sat, 31 Dec 2022 10:40:39 +1100 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=81=E2=80=8D=E2=99=82=EF=B8=8F=20close?= =?UTF-8?q?=20#9684=20revert=20`UnPackAsyncDefaultValues`=20to=20avoid=20T?= =?UTF-8?q?S=20breaking=20change=20(#9686)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- reports/api-extractor.md | 25 +++++++++++-------------- src/__tests__/type.test.tsx | 22 +++++++++++++--------- src/__tests__/useForm.test.tsx | 8 ++++++-- src/logic/createFormControl.ts | 4 +--- src/types/errors.ts | 10 ++-------- src/types/form.ts | 17 +++++------------ src/types/path/eager.ts | 19 ++++++------------- src/types/utils.ts | 3 --- 8 files changed, 44 insertions(+), 64 deletions(-) diff --git a/reports/api-extractor.md b/reports/api-extractor.md index ea50cafaeb5..fdc33a2d4ae 100644 --- a/reports/api-extractor.md +++ b/reports/api-extractor.md @@ -75,7 +75,7 @@ export type Control> = Path>>(props: ControllerProps) => ReactElement>; +export const Controller: = Path>(props: ControllerProps) => ReactElement>; // @public (undocumented) export type ControllerFieldState = { @@ -182,10 +182,10 @@ export type FieldArrayMethodProps = { }; // @public -export type FieldArrayPath = ArrayPath>; +export type FieldArrayPath = ArrayPath; // @public -export type FieldArrayPathValue> = PathValue, TFieldArrayPath>; +export type FieldArrayPathValue> = PathValue; // @public export type FieldArrayWithId = FieldArrayPath, TKeyName extends string = 'id'> = FieldArray & Record; @@ -203,7 +203,7 @@ export type FieldError = { }; // @public (undocumented) -export type FieldErrors = Partial ? any : FieldErrorsImpl>>>; +export type FieldErrors = Partial ? any : FieldErrorsImpl>>; // @public (undocumented) export type FieldErrorsImpl = { @@ -217,7 +217,7 @@ export type FieldName = IsFlatObject = DeepMap, boolean>; // @public -export type FieldPath = Path>; +export type FieldPath = Path; // @public export type FieldPathByValue = { @@ -225,7 +225,7 @@ export type FieldPathByValue = { }[FieldPath]; // @public -export type FieldPathValue> = PathValue, TFieldPath>; +export type FieldPathValue> = PathValue; // @public export type FieldPathValues[] | readonly FieldPath[]> = {} & { @@ -259,9 +259,9 @@ export type FormState = { isValidating: boolean; isValid: boolean; submitCount: number; - defaultValues?: UnPackAsyncDefaultValues | undefined | Readonly>; - dirtyFields: Partial>>>; - touchedFields: Partial>>>; + defaultValues?: undefined | Readonly>; + dirtyFields: Partial>>; + touchedFields: Partial>>; errors: FieldErrors; }; @@ -488,9 +488,6 @@ export type TriggerConfig = Partial<{ shouldFocus: boolean; }>; -// @public (undocumented) -export type UnPackAsyncDefaultValues = TFieldValues extends () => Promise ? U : TFieldValues; - // @public @deprecated (undocumented) export type UnpackNestedValue = T extends NestedValue ? U : T extends Date | FileList | File | Blob ? T : T extends object ? { [K in keyof T]: UnpackNestedValue; @@ -594,7 +591,7 @@ export type UseFormGetValues = { export type UseFormHandleSubmit = (onValid: SubmitHandler, onInvalid?: SubmitErrorHandler) => (e?: React_2.BaseSyntheticEvent) => Promise; // @public (undocumented) -export type UseFormProps = UnPackAsyncDefaultValues, TContext = any> = Partial<{ +export type UseFormProps = Partial<{ mode: Mode; reValidateMode: Exclude; defaultValues: DefaultValues | AsyncDefaultValues; @@ -779,7 +776,7 @@ export type WatchObserver = (value: DeepPartia // Warnings were encountered during analysis: // // src/types/form.ts:97:3 - (ae-forgotten-export) The symbol "AsyncDefaultValues" needs to be exported by the entry point index.d.ts -// src/types/form.ts:432:3 - (ae-forgotten-export) The symbol "Subscription" needs to be exported by the entry point index.d.ts +// src/types/form.ts:425:3 - (ae-forgotten-export) The symbol "Subscription" needs to be exported by the entry point index.d.ts // (No @packageDocumentation comment for this package) diff --git a/src/__tests__/type.test.tsx b/src/__tests__/type.test.tsx index 43656a8ba4d..4356ea06b12 100644 --- a/src/__tests__/type.test.tsx +++ b/src/__tests__/type.test.tsx @@ -287,6 +287,14 @@ test('should work with generic component path assertion', () => { }); test('should infer async default values', () => { + const formValues = { + test: 'test', + test1: { + nested: 'test', + }, + fieldArray: [{ test: '' }], + }; + function App() { const { register, @@ -302,15 +310,9 @@ test('should infer async default values', () => { setFocus, trigger, setError, - } = useForm({ + } = useForm({ defaultValues: async () => { - return { - test: 'test', - test1: { - nested: 'test', - }, - fieldArray: [{ test: '' }], - }; + return formValues; }, }); useFieldArray({ @@ -334,7 +336,9 @@ test('should infer async default values', () => { setValue('test1.nested', 'data'); reset({ test: 'test', - test1: 'test1', + test1: { + nested: 'test1', + }, }); watch('test'); diff --git a/src/__tests__/useForm.test.tsx b/src/__tests__/useForm.test.tsx index 6760189f02e..4b8bd3553bc 100644 --- a/src/__tests__/useForm.test.tsx +++ b/src/__tests__/useForm.test.tsx @@ -1724,7 +1724,9 @@ describe('useForm', () => { const { register, formState: { isLoading }, - } = useForm({ + } = useForm<{ + test: string; + }>({ defaultValues: async () => { await sleep(100); @@ -1761,7 +1763,9 @@ describe('useForm', () => { it('should update async default values for controlled components', async () => { const App = () => { - const { control } = useForm({ + const { control } = useForm<{ + test: string; + }>({ defaultValues: async () => { await sleep(100); diff --git a/src/logic/createFormControl.ts b/src/logic/createFormControl.ts index 1335c706e1b..55435eaf42b 100644 --- a/src/logic/createFormControl.ts +++ b/src/logic/createFormControl.ts @@ -20,7 +20,6 @@ import { SetFieldValue, SetValueConfig, Subjects, - UnPackAsyncDefaultValues, UseFormClearErrors, UseFormGetFieldState, UseFormGetValues, @@ -585,8 +584,7 @@ export function createFormControl< true, ); - options.shouldValidate && - trigger(name as Path>); + options.shouldValidate && trigger(name as Path); }; const setValues = < diff --git a/src/types/errors.ts b/src/types/errors.ts index 015623ab554..86005c0d4ca 100644 --- a/src/types/errors.ts +++ b/src/types/errors.ts @@ -1,11 +1,5 @@ import { FieldValues, InternalFieldName, Ref } from './fields'; -import { - BrowserNativeObject, - IsAny, - LiteralUnion, - Merge, - UnPackAsyncDefaultValues, -} from './utils'; +import { BrowserNativeObject, IsAny, LiteralUnion, Merge } from './utils'; import { RegisterOptions, ValidateResult } from './validator'; export type Message = string; @@ -47,7 +41,7 @@ export type FieldErrorsImpl = { export type FieldErrors = Partial< FieldValues extends IsAny ? any - : FieldErrorsImpl>> + : FieldErrorsImpl> >; export type InternalFieldErrors = Partial< diff --git a/src/types/form.ts b/src/types/form.ts index d0ab3b8e429..676769621b5 100644 --- a/src/types/form.ts +++ b/src/types/form.ts @@ -18,7 +18,7 @@ import { FieldPathValues, } from './path'; import { Resolver } from './resolvers'; -import { DeepMap, DeepPartial, Noop, UnPackAsyncDefaultValues } from './utils'; +import { DeepMap, DeepPartial, Noop } from './utils'; import { RegisterOptions } from './validator'; declare const $NestedValue: unique symbol; @@ -89,7 +89,7 @@ type AsyncDefaultValues = ( ) => Promise; export type UseFormProps< - TFieldValues extends UnPackAsyncDefaultValues = UnPackAsyncDefaultValues, + TFieldValues extends FieldValues = FieldValues, TContext = any, > = Partial<{ mode: Mode; @@ -131,16 +131,9 @@ export type FormState = { isValidating: boolean; isValid: boolean; submitCount: number; - defaultValues?: - | UnPackAsyncDefaultValues - | undefined - | Readonly>; - dirtyFields: Partial< - Readonly>> - >; - touchedFields: Partial< - Readonly>> - >; + defaultValues?: undefined | Readonly>; + dirtyFields: Partial>>; + touchedFields: Partial>>; errors: FieldErrors; }; diff --git a/src/types/path/eager.ts b/src/types/path/eager.ts index aff596df7f6..5ebdebdb98e 100644 --- a/src/types/path/eager.ts +++ b/src/types/path/eager.ts @@ -1,9 +1,5 @@ import { FieldValues } from '../fields'; -import { - BrowserNativeObject, - Primitive, - UnPackAsyncDefaultValues, -} from '../utils'; +import { BrowserNativeObject, Primitive } from '../utils'; import { ArrayKey, IsTuple, TupleKeys } from './common'; @@ -38,9 +34,7 @@ export type Path = T extends ReadonlyArray /** * See {@link Path} */ -export type FieldPath = Path< - UnPackAsyncDefaultValues ->; +export type FieldPath = Path; /** * Helper type for recursively constructing paths through a type. @@ -78,9 +72,8 @@ export type ArrayPath = T extends ReadonlyArray /** * See {@link ArrayPath} */ -export type FieldArrayPath = ArrayPath< - UnPackAsyncDefaultValues ->; +export type FieldArrayPath = + ArrayPath; /** * Type to evaluate the type which the given path points to. @@ -118,7 +111,7 @@ export type PathValue | ArrayPath> = T extends any export type FieldPathValue< TFieldValues extends FieldValues, TFieldPath extends FieldPath, -> = PathValue, TFieldPath>; +> = PathValue; /** * See {@link PathValue} @@ -126,7 +119,7 @@ export type FieldPathValue< export type FieldArrayPathValue< TFieldValues extends FieldValues, TFieldArrayPath extends FieldArrayPath, -> = PathValue, TFieldArrayPath>; +> = PathValue; /** * Type to evaluate the type which the given paths point to. diff --git a/src/types/utils.ts b/src/types/utils.ts index 073357414aa..8f00d1f7f7b 100644 --- a/src/types/utils.ts +++ b/src/types/utils.ts @@ -53,9 +53,6 @@ export type DeepPartialSkipArrayKey = T extends ? { [K in keyof T]: DeepPartialSkipArrayKey } : { [K in keyof T]?: DeepPartialSkipArrayKey }; -export type UnPackAsyncDefaultValues = - TFieldValues extends () => Promise ? U : TFieldValues; - /** * Checks whether the type is any * See {@link https://stackoverflow.com/a/49928360/3406963}