react-final-form icon indicating copy to clipboard operation
react-final-form copied to clipboard

FieldRenderProps type incorrectly includes an index signature

Open jsonunger opened this issue 3 years ago • 0 comments

Are you submitting a bug report or a feature request?

What is the current behavior?

FieldRenderProps includes an index signature that incorrectly assumes the matching object can have any number of fields beyond input and meta. If you define a component whose props extend FieldRenderProps, you end up including the index signature which allows any prop to be passed into the component even if it is untyped and unused. This is not type safe and can result in missed prop handling.

What is the expected behavior?

FieldRenderProps should not have an index signature included in its type since input and meta are the only two values explicitly defined within the field.

Sandbox Link

TypeScript sandbox with incorrect behavior

What's your environment?

React Final Form v6.5.9 Final Form v4.20.7 TypeScript v4.6.2

Other information

The FieldRenderProps interface includes an index signature. However, this index signature is unnecessary to how the type is used and results in poor type safety when used.

The interface is used for two pieces of the API, the Field component and the useField hook.

The useField type returns FieldRenderProps but the code explicitly returns only input and meta. Thus, the index signature serves no purpose here.

The Field component uses FieldRenderProps to ensure that the props of the rendered component extend FieldRenderProps. In this context, the index signature also doesn't provide any benefit because a type with extra props will still extend FieldRenderProps (example TS sandbox)

jsonunger avatar May 09 '22 15:05 jsonunger