0

I have one component for example

export class Foo {
@Input() myInput:string = 'a';

}

Now I want the allowed values of myInput to be any of these only: a , b, c, d

if someone tries to do

<foo myInput="x">

He should get an compilation error

I believe this is possible

Thanks

1 Answer 1

3

What you are trying to achieve can be done by

export class Foo {
@Input() myInput: ’a’ | ‘b’ | ‘c’ | ‘d’;

} 

By doing this you are explicitly telling TypeScript to only allow those specific characters for the binding myInput

You can get a bit more detail on type guards and type definitions in the documentation

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.