I'm currently facing an issue where I need to assign a value from a typed object that accepts various dynamic values to a boolean class instance member property. The object has a boolean value set, but when trying to assign it to the class property, I encounter an error...
interface Cat {
[ index: string ] : string | number | boolean
}
class Test {
private result : boolean | undefined;
doIt () {
let c: Cat = { purring: true }
this.result = cat.purring;
}
}
The error occurs on the line attempting to set this.result
:
Type 'string | number | boolean' is not assignable to type 'boolean | undefined'. Type 'string' is not assignable to type 'boolean | undefined'.
I found that I only seem to be able to resolve this by explicitly casting the value as boolean:
this.result = cat.purring as boolean;
However, I would prefer to avoid this if possible... ? ...
In addition to this issue, I am also facing a similar problem while trying to allow either an array of strings or an array of arrays of strings:
type StringOrNestedString = string[] | StringOrNestedString[]
interface Item {
filters: StringOrNestedString,
}
let i: Item = {
filters: ['foo', ['bar']],
}
The error message received is:
Type '(string | string[])[]' is not assignable to type 'StringOrNestedString'. Type '(string | string[])[]' is not assignable to type 'string[]'.