-
|
I asked a similar question here (#548), but this one is slightly different. Let's say I have some args: interface Args {
foo: string | number;
}I want to do an if and pass string or number to the right child component, so I create a getter to check the type: get isFooString() {
return typeof this.args.foo === 'string';
}… and use it in the template: … but the type when passing to the component is still They don't seem to be supported as additional special forms! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
What you're looking for is a TS feature called type predicates. What you can do is define a method/helper whose return value is a type predicate: isString(value: unknown): value is string {
return typeof value === 'string';
}And then use that in your template: Note that due to limitations of the type system itself, it's not possible to express a helper whose return type is a type predicate using |
Beta Was this translation helpful? Give feedback.
-
|
Nice! I will try that out. I had tried the helper and could not get to it to work which makes sense. Thank you!
|
Beta Was this translation helpful? Give feedback.
What you're looking for is a TS feature called type predicates. What you can do is define a method/helper whose return value is a type predicate:
And then use that in your template:
Note that due to limitations of the type system itself, it's not possible to express a helper whose return type is a type predicate using
helper()/extends Helper—you have to be using a function directly.