Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to set a Date type #149

Closed
Hypenate opened this issue Mar 3, 2022 · 2 comments
Closed

Unable to set a Date type #149

Hypenate opened this issue Mar 3, 2022 · 2 comments

Comments

@Hypenate
Copy link
Contributor

Hypenate commented Mar 3, 2022

Is this a regression?

No

Description

I'm unable to create a FormGroup with a type that contains a Date.

export type Person = {
  id: string,
  birthday: Date
}
  private createForm() {
    // Type 'Date' is not assignable to type 'ControlsOf<Date>'.
    const form1 = new FormGroup<ControlsOf<Person>>({
      id: new FormControl<Person["id"]>(),
      birthday: new FormGroup<Person["birthday"]>(new Date)
    })

    // Type 'FormGroup<Date>' is not assignable to type 'FormGroup<ControlsOf<Date>>'. 
    const form2 = new FormGroup<ControlsOf<Person>>({
      id: new FormControl<Person["id"]>(),
      birthday: new FormGroup<Date>(new Date)
    })

    // Argument of type 'Date' is not assignable to parameter of type 'ControlsOf '.
    const form3 = new FormGroup<ControlsOf<Person>>({
      id: new FormControl<Person["id"]>(),
      birthday: new FormGroup<ControlsOf<Person["birthday"]>>(new Date)
    })

    // Argument of type 'Date' is not assignable to parameter of type 'ControlsOf '.
    const form4 = new FormGroup<ControlsOf<Person>>({
      id: new FormControl<Person["id"]>(),
      birthday: new FormGroup<ControlsOf<Date>>(new Date)
    })

    // Conversion of type 'Date' to type 'ControlsOf<Date>' may be a mistake.
    const form5 = new FormGroup<ControlsOf<Person>>({
      id: new FormControl<Person["id"]>(),
      birthday: new FormGroup<ControlsOf<Date>>(new Date as ControlsOf<Date>)
    })
  }
}

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in

Angular: 13.2.4
"@ngneat/reactive-forms": "4.0.4",

Anything else?

No response

Do you want to create a pull request?

No

@NetanelBasal
Copy link
Member

#109 (comment)

@Hypenate
Copy link
Contributor Author

Hypenate commented Mar 4, 2022

This indeed seems to work:

  export type Person = {
    id: string,
    birthday: FormControl<Date>;
  }
  const form3 = this.fb.group<Person>({
    id: 'id',
    birthday: this.fb.control(new Date)
  })

But it doesn't feel intuative.
I need to update my type to something that doesn't reflect the 'reallity'.

If I update my type to:

  export type Person = {
    id: string,
    birthday: Date | string;
  }

Then this becomes valid again:

  const form1 = this.fb.group<ControlsOf<Person>>({
    id: new FormControl('id'),
    birthday: new FormControl(new Date())
  })

Which makes a lot more sense, but it's stange that the compiler does not complain about this 🤔

@ngneat ngneat locked and limited conversation to collaborators Mar 5, 2022
@NetanelBasal NetanelBasal converted this issue into discussion #153 Mar 5, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants