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
Template compiler should treat TypeScript !
on inputs as required inputs
#24879
Comments
Duplicate of #11904 This is totally against TypeScript semantics, the Also could you try demo what TypeScript code should template compiler generate to support that checked? Even in AOT? |
How does this go against TypeScript semantics any more than the way Angular uses decorators on classes ( The template compiler should not generate any new code, it should generate new errors (if you use a component in a template and omit a required input for it), as my link demonstrates. This is a template compile-time check, like most template compile-time checks, which may only work in AoT mode, like some other template compile-time checks. |
Decorator is a JavaScript feature which is fully runtime-based. Angular could strip it because it's defined by Angular and have no other semantic.
This information doesn't exist in either |
It is unfortunate that this isn't output in |
First of all, there’s no difference in metadata processing between AOT and JIT, only metadata retrieval. No difference can be made in how metadata looks like between AOT and JIT. Your proposal is going to against that. Second, there will be no more
It is. Angular doesn’t have any additional type-check in AOT, but typescript does. So I asked what kind of TypeScript code should it generate to enable that check, since Angular won’t do it. |
It could change So in AoT mode, |
It won't, the metadata for same code must stay same in both AOT and JIT. |
TypeScript can make a program not-compile by throwing a type error, but it can't make a program behave differently. In other words if you put Closing as not implementable. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm submitting a...
Current behavior
Currently, you cannot mark an
@Input()
as required a template compile time, you can just assert it is not undefined at runtime.TypeScript supports (or requires, if you use
strictPropertyInitialization
) adding a!
to the end of instance property declarations which aren't initialized in-line or in the constructor to communicate to the type-checker that this variable is indeed defined, even if it doesn't appear to be.The combination of the above two points means you end up with a sort of inconsistent state, in which you want to assert your inputs are defined in ngOnInit or ngOnChanges, yet the type-checker thinks they are already defined because you used the
!
operator on their definitions.Expected behavior
It would be nice if the template compiler could detect this operator on declarations of inputs, and take it to mean that input is required. This means the template compiler would give a (helpful) error if you used such a component but did not specify one of these required inputs, avoiding such mistakes at compile time instead of at runtime.
Minimal reproduction of the problem with instructions
http://plnkr.co/edit/7wr39D8FiEIb6JCteXr1?p=preview
(stackblitz doesn't support a new enough version of TypeScript, so I'm using plnkr)
What is the motivation / use case for changing the behavior?
In general, it would be useful to have a required parameter check as a compile-time check instead of a runtime check to both catch mistakes earlier and to reduce/simplify code (no manual checks and having to deal with checking in both ngOnInit and onChanges).
This is especially true now that TypeScript supports
strictPropertyInitialization
, which makes the aforementioned checks less idiomatic for TypeScript to do (since you assume the value is defined but check if it isn't defined).Environment
The text was updated successfully, but these errors were encountered: