2

I am using Angular 9 and have following Angular directive :

export type MyDirectiveValue = "foo"|"bar"; 
@Directive({
    selector: '[myDirective]'
})
export class MyDirective implements OnInit {
    @Input("myDirective") value: MyDirectiveValue;
    
    ngOnInit() {
        console.log(this.value);
    }
}

When I use it in my template :

<div *myDirective="'invalid-value'">Hello</div>

I would have expected Ivy to detect that invalid-value is not assignable to type MyDirectiveValue but it's not, I can freely assign any value without getting any template compilation failure.

Any idea ?

3
  • 2
    I think this may help: angular.io/guide/template-typecheck check out fullTemplateTypeCheck Commented Feb 19, 2021 at 21:26
  • @FaustynPiechowiak fullTemplateTypeCheck was already set to true in my case, but the documentation was helpful as when I introduced strictTemplates: true it fixed my issues, thanks a lot. BTW, if you want to propose the answer, I may accept if to share some kudos ;-) Commented Feb 19, 2021 at 21:46
  • Sure. Very kind of you, thanks! :) Commented Feb 21, 2021 at 10:05

1 Answer 1

2

Please check out documentation: https://angular.io/guide/template-typecheck

Especially this part:

Strict mode

Strict mode is a superset of full mode, and is accessed by setting the strictTemplates flag to true. This flag supersedes the fullTemplateTypeCheck flag. In strict mode, Angular version 9 adds checks that go beyond the version 8 type-checker. Note that strict mode is only available if using Ivy.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.