Required @Input()
See original GitHub issueWhich @angular/* package(s) are relevant/related to the feature request?
No response
Description
Inputs in Angular arent required at all and bugs are cached while manually testing the code. We could write for every component checks if the object will be present onInit if its null etc. From my personall experience this leads to bigger and less maintainable code structure.
Proposed solution
I suggest angulars takes a look to add either a new pipe @Required - which I have an example for, or extend the existing one @Input to additionally provide a object with meta data if its required, what value are allowed etc.
@RequiredInputs()
@Component({
...
})
export class MyComponent {
readonly data$ = new ReplaySubject<string[] | undefined>();
@Input() @Required() set data(data: string[] | undefined) { // By default undefined, null and NaN are forbidden. To change behavior just pass config.
this.data$.next(data);
}
}
Alternatives considered
My code currently works for observables but the exact same behavior can be built for basic input fields.
For more see those files: https://github.com/muhamedkarajic/angular-playground/tree/main/src/app/shared/decorators
P.S:
In order to extend existing decorator @Input() we should make the current string be of an string | object type. If an object is passed inside the object can be the proper input identifier and if inside that object there is no identifier it would (as currently) use the property name. If there isn’t any value passed or a string is passed (as now) it would fall back to the current behavior - that way nothing would break.
Issue Analytics
- State:
- Created a year ago
- Comments:7 (3 by maintainers)
Top GitHub Comments
@thw0rted we want to centralize the discussion about required inputs in one place, regardless of the proposed solution. Generally speaking we would like to have an issue that focuses on the problem statement as there are multiple approaches and we are not settling on any of the options just yet.
@muhamedkarajic The proposed solution may be different but the feature request is the same: Provide a way to define required inputs. How this gets implemented may or may not be any of the proposed solutions. That said, we really want to consolidate all discussions rather than having 4 different issues open for each feature request.