Skip to content

@types-redux-form can we remove the Partial for initialValues in the reduxForm Hoc? #24376

Description

@Rockson

At the moment using reduxForm Hoc with typescript i can define the "initialValues" data type that the component expect. But that type will be a Partial of the type that you define.
But couldn't we let the user define if a property can be undefined or not instead of making all the fields undefined?

I'll explain my use case:

// Form.tsx

interface FormData {
  name: string
  surname: string
}

class MyComponent extends React.Component<InjectedFormProps<FormFields, {}>{
   render() {
   (...)
   }
}


// This is where the Partial is applied, it will assign to initialValues a type of Partial<FormData> instead of FormData
export default reduxForm<FormData, {}>({
  form: 'myForm'
})(MyComponent)


// FormPage.tsx

interface GraphqlProps {
  data: QueryProps & CurrentUserQuery
}

const CurrentUserForm = graphql(currentUserQuery, 
({data: { currentUser }}: GraphqlProps) => (
  <Form initialValues={currentUser} />
))

export const FormPage = () => <CurrentUserForm />

// GeneratedGraphqlTypes
// This file is auto-generated using the apollo-codegen utility
interface CurrentUserQuery {
  currentUser: {
    name: string
    surname: string | null
  } | null
}

U see that if for any reason i have to change the result of my graphql query so, for example if I remove the surname field, the assignment of initialValues in the component CurrentUserForm will throw a compile error.

But if we keep the Partial in the hoc reduxForm the error won't be thrown and I'll have no way to know that I have to change that file (if not by remembering to do it or to re-check any part of the code or with a global search, but you know, It's not the same)

Probably the only change needed is in the interfaces InjectedFormProps and ConfigProps . Where i can see:

initialValues?: Partial<FormData>;

Where the Partial should be removed

But I don't know if that will break something else. If it's ok for you I would do a pull request

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions