question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Canonical: Marking @Input as required

See original GitHub issue

šŸš€ feature request

Relevant Package

This feature request is for @angular/core

Description

There is a long history of discussion about whether / how to have the compiler enforce that specific inputs are provided to components: #11904 #18156 #24879 #28403 #30974

However, all of those are now closed and locked. They contain some advice about how to mitigate the problem, or kick the can down the road (ā€œOne day I hope weā€™ll be able to do this. That day is not today.ā€), but thanks to the lock-bot none of them can be used to continue the conversation, so I guess we need yet another issue, because I believe the problem still exists.

Describe the solution youā€™d like

It should be possible to annotate inputs such that using a component in a template without providing a value for that input is a compile-time error. Template checking has come a long way, and itā€™s already possible for the compiler to flag providing the wrong type as an argument. Combine with the strictPropertyInitializers flag, and such a check could rely on the existing ! assertion operator.

Describe alternatives youā€™ve considered

The best approach Iā€™ve seen while reviewing the above issues is specifying the required attributes in the selector component metadata, but a) apparently that wonā€™t catch passing null or undefined to an Input that not defined with a type that includes those values, and b) that appears to break certain test harnesses? Also, I havenā€™t started using strictTemplates / fullTemplateTypeCheck yet due to some problems in previous versions with narrowing on *ngIf that may or may not have been resolved ā€“ those existing capabilities may address some or all of this need.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:54
  • Comments:25 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
cyrilletuzicommented, Jun 25, 2020

Combine with the strictPropertyInitializers flag, and such a check could rely on the existing ! assertion operator.

For information, it is what Ionicā€™s Stencil is doing.

4reactions
ratoi-crystycommented, Jan 11, 2021

Iā€™m wondering though if adding the input names to the directive selector is actually the official recommendation to achieve the goal - or rather a hackish workaround. If it is recommended - maybe it should be fixed as such in the docs?

Even if you fix the docs, and start promoting this approach, I still think itā€™s not ok. First, the error will be wrong, as the compiler will complain about not finding the component, instead of the desired error. Secondly, if you leave the selector, without the attribute, and use the component. You will end up in with a strange behavior. If you pass an undefined value, the compiler will complain, but if you skip the attribute, it will work, even though the runtime behavior is the same.

Thatā€™s why I think, the best approach is to treat them the same, meaning that an error should occur, similar to passing an undefined value, as thatā€™s how the runtime behavior is.

Edit: The above example is reproducible in strict mode, Iā€™m not sure if it works outside it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Marking Required Fields in Forms - Nielsen Norman Group
The solution is simple: mark all the required fields. Be as explicit and transparent as possible: for every single field that must be...
Read more >
required - CSS-Tricks
Simply having required on an element lets the browser know this attribute exists and the corresponding input is in fact a required field....
Read more >
What Is a Canonical Data Model? CDMs Explained
Integrating and sharing data across all your systems is complicated. The Canonical Data Model might be your solution. Learn more here.
Read more >
How to set HTML5 required attribute in Javascript?
If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for...
Read more >
Canonical XML Version 1.1 - W3C
If a canonical form contains comments corresponding to the comment nodes in the input node-set, the result is called canonical XML with commentsĀ ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found