Skip to content
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

what if model has conditional properties? #64

Closed
smorandi opened this issue Oct 28, 2021 · 5 comments
Closed

what if model has conditional properties? #64

smorandi opened this issue Oct 28, 2021 · 5 comments

Comments

@smorandi
Copy link

smorandi commented Oct 28, 2021

assume the following:

interface Model {
 validFrom: string,
  validTo?: string
}

so i tried to do the following and ran into multiple issues i am not sure how to deal with, or rather what could be done about it for a proper solution?

interface Model {
  validFrom: string,
  validTo?: string
}

const factory = (initialModel: Model) => typedFormGroup<Model>({
  validFrom: typedFormControl(initialModel.validFrom),


  // does not work...?
  // validTo: typedFormControl(initialModel.validTo),

  // this works...
  validTo: typedFormControl<string | undefined>(initialModel.validTo),

});

const mfg = factory({validFrom: 'foo', validTo: 'bar'});

// getting error on controls.validTo, since it can be undefined???
combineLatest([mfg.controls.validFrom.valueChanges, mfg.controls.validTo.valueChanges]).pipe(
  //something...
).subscribe();

can you tell me if i am doing something wrong?

@gparlakov
Copy link
Owner

gparlakov commented Oct 28, 2021 via email

@smorandi
Copy link
Author

well yes, but the point is that a validTo controller is always available, it should never be undefined. maybe when creating the types they should omit any conditionals first.

like this maybe https://www.typescriptlang.org/docs/handbook/2/mapped-types.html

removing the optional types for when you create a type of the model mapped to the individual controllers…

// Removes 'optional' attributes from a type's properties
type Concrete<Type> = {
  [Property in keyof Type]-?: Type[Property];
};

just an idea though…

@gparlakov
Copy link
Owner

gparlakov commented Nov 6, 2021

Hey, @smorandi it seems I've misunderstood your initial concern.

If I understand, the validTo property of the form.controls should not be undefined if we've defined it in the typedFormGroup function call - right? The following should not break:

image

It seems there is a utility type coming from TS itself that can help: Required:

interface ModelWithPartial {
  validFrom: string;
  validTo?: string;
}

// const factory = (initialModel: ModelWithPartial) => typedFormGroup<ModelWithPartial>({
const factory = (initialModel: ModelWithPartial) => typedFormGroup<Required<ModelWithPartial>>({
  validFrom: typedFormControl(initialModel.validFrom),


  // does not work...?
  // validTo: typedFormControl(initialModel.validTo),

  // this works...
  validTo: typedFormControl(initialModel.validTo),

});

const mfg = factory({validFrom: 'foo', validTo: 'bar'});

// getting error on controls.validTo, since it can be undefined???
// not getting an error any more
mfg.controls.validTo.valueChanges.subscribe(v => v?.includes('t'));

old, misunderstood-question answer:

Hey, @smorandi that looks like it might remove the optional properties in general. So when you try to actually use them in the form (or another form) you'll not get the strong-type.

Edit: I looked into that type and it does not remove the properties, it removes the optionality:
image

Still this -? trick was new to me so - thanks 👍🏻

My understanding is that the idea is to have the typedFormGroup function's parameter skip some of the formControl-s in its initializer, like so:

image

That would cause the issue I mentioned above - wherever one uses the optional props (like names and age in the above case) they'd have to check for null/undefined, which might be awkward - especially in a template.

@gparlakov
Copy link
Owner

@smorandi closing this issue. Let me know if there's more I can do to help.

@smorandi
Copy link
Author

oh wow, thx, did not expect anything 😂 but great!

well never mind, i guess with Angular 14 theres a new approach coming for more strongly typed forms it anyway. guess i will have a look once its out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants