Skip to content

Commit 90cdaee

Browse files
🐞 fix: add proper types to form.subscribe (#12850)
1 parent cff8819 commit 90cdaee

File tree

5 files changed

+18
-19
lines changed

5 files changed

+18
-19
lines changed

reports/api-extractor.md.api.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ export type FormSubmitHandler<TTransformedValues> = (payload: {
342342
}) => unknown | Promise<unknown>;
343343

344344
// @public (undocumented)
345-
export type FromSubscribe<TFieldValues extends FieldValues> = (payload: {
346-
name?: string;
345+
export type FromSubscribe<TFieldValues extends FieldValues> = <TFieldNames extends readonly FieldPath<TFieldValues>[]>(payload: {
346+
name?: readonly [...TFieldNames] | TFieldNames[number];
347347
formState?: Partial<ReadFormState>;
348348
callback: (data: Partial<FormState<TFieldValues>> & {
349349
values: TFieldValues;
@@ -802,8 +802,8 @@ export type UseFormWatch<TFieldValues extends FieldValues> = {
802802
};
803803

804804
// @public
805-
export type UseFromSubscribe<TFieldValues extends FieldValues> = (payload: {
806-
name?: string;
805+
export type UseFromSubscribe<TFieldValues extends FieldValues> = <TFieldNames extends readonly FieldPath<TFieldValues>[]>(payload: {
806+
name?: readonly [...TFieldNames] | TFieldNames[number];
807807
formState?: Partial<ReadFormState>;
808808
callback: (data: Partial<FormState<TFieldValues>> & {
809809
values: TFieldValues;
@@ -893,7 +893,7 @@ export type WatchObserver<TFieldValues extends FieldValues> = (value: DeepPartia
893893

894894
// Warnings were encountered during analysis:
895895
//
896-
// src/types/form.ts:479:3 - (ae-forgotten-export) The symbol "Subscription" needs to be exported by the entry point index.d.ts
896+
// src/types/form.ts:481:3 - (ae-forgotten-export) The symbol "Subscription" needs to be exported by the entry point index.d.ts
897897

898898
// (No @packageDocumentation comment for this package)
899899

src/logic/shouldSubscribeByName.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import convertToArrayPayload from '../utils/convertToArrayPayload';
22

3-
export default <T extends string | string[] | undefined>(
3+
export default <T extends string | readonly string[] | undefined>(
44
name?: T,
55
signalName?: string,
66
exact?: boolean,

src/types/form.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,10 @@ useEffect(() => {
391391
})
392392
* ```
393393
*/
394-
export type UseFromSubscribe<TFieldValues extends FieldValues> = (payload: {
395-
name?: string;
394+
export type UseFromSubscribe<TFieldValues extends FieldValues> = <
395+
TFieldNames extends readonly FieldPath<TFieldValues>[],
396+
>(payload: {
397+
name?: readonly [...TFieldNames] | TFieldNames[number];
396398
formState?: Partial<ReadFormState>;
397399
callback: (
398400
data: Partial<FormState<TFieldValues>> & { values: TFieldValues },
@@ -790,8 +792,10 @@ export type BatchFieldArrayUpdate = <
790792
shouldUpdateFieldsAndErrors?: boolean,
791793
) => void;
792794

793-
export type FromSubscribe<TFieldValues extends FieldValues> = (payload: {
794-
name?: string;
795+
export type FromSubscribe<TFieldValues extends FieldValues> = <
796+
TFieldNames extends readonly FieldPath<TFieldValues>[],
797+
>(payload: {
798+
name?: readonly [...TFieldNames] | TFieldNames[number];
795799
formState?: Partial<ReadFormState>;
796800
callback: (
797801
data: Partial<FormState<TFieldValues>> & { values: TFieldValues },

src/useFormState.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import React from 'react';
22

33
import getProxyFormState from './logic/getProxyFormState';
4-
import {
5-
FieldValues,
6-
InternalFieldName,
7-
UseFormStateProps,
8-
UseFormStateReturn,
9-
} from './types';
4+
import { FieldValues, UseFormStateProps, UseFormStateReturn } from './types';
105
import { useFormContext } from './useFormContext';
116
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
127

@@ -63,7 +58,7 @@ export function useFormState<
6358
useIsomorphicLayoutEffect(
6459
() =>
6560
control._subscribe({
66-
name: name as InternalFieldName,
61+
name,
6762
formState: _localProxyFormState.current,
6863
exact,
6964
callback: (formState) => {

src/useWatch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export function useWatch<
145145
export function useWatch<TFieldValues extends FieldValues>(
146146
props?: UseWatchProps<TFieldValues>,
147147
) {
148-
const methods = useFormContext();
148+
const methods = useFormContext<TFieldValues>();
149149
const {
150150
control = methods.control,
151151
name,
@@ -164,7 +164,7 @@ export function useWatch<TFieldValues extends FieldValues>(
164164
useIsomorphicLayoutEffect(
165165
() =>
166166
control._subscribe({
167-
name: name as InternalFieldName,
167+
name,
168168
formState: {
169169
values: true,
170170
},

0 commit comments

Comments
 (0)