Giter Site home page Giter Site logo

Comments (5)

gparlakov avatar gparlakov commented on May 21, 2024

from forms-typed.

smorandi avatar smorandi commented on May 21, 2024

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…

from forms-typed.

gparlakov avatar gparlakov commented on May 21, 2024

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.

from forms-typed.

gparlakov avatar gparlakov commented on May 21, 2024

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

from forms-typed.

smorandi avatar smorandi commented on May 21, 2024

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.

from forms-typed.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.