Comments (5)
from forms-typed.
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.
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:
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:
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:
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.
@smorandi closing this issue. Let me know if there's more I can do to help.
from forms-typed.
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)
- Add the Angular 2.4.0 using ngx-forms-typed example
- Make npm package `ngx-show-form-component` HOT 1
- Correct Readme link for ngx-forms-typed
- Correct readme link for ngx-show-form-control
- A typed form array of typed from groups HOT 3
- Support Ivy
- npm package is not the production build HOT 4
- Add contributors page
- Support the full FormControl API HOT 4
- Order the TypedArray types
- Allow for {disabled: true, value: T} in the FormControl values HOT 1
- Incorrect README install instructions HOT 3
- NPM page missing license HOT 1
- `FormGroup.getRawValue()` is still `any` HOT 2
- if form group has multiple array? how to add TypedArraysIn? HOT 4
- Adjustments to nested typedFormArray of typedFormGroups usage HOT 7
- forms-utils: add the async validators up the chain HOT 1
- Hey, what do you think about that solution? HOT 2
- Migration to angular 14+ HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from forms-typed.