I have an object:
interface MYInterface {
aaa: number;
bbb: number;
ccc?: number | undefined;
}
const myObject: MYInterface = {
aaa: 0,
bbb: 0,
ccc: 132,
};
I want to check if some keys in this object, satisfy a condition! I'm using Array.some(...) like below:
const res = ['aaa', 'bbb'].some((key) => myObject[key] > 0)
but for myObject[key]
I'm getting a TypeScript error about:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'INxStateCounts'.
No index signature with a parameter of type 'string' was found on type 'INxStateCounts'.
I know that what am I trying to do is checking array of strings (keys) with objects but my array contains those keys in string.
I have tried to cast key as keyof MYInterface
but no luck! I was getting so many other errors. How can I fix this?
Also my object is very big, here I use 3 properties to demonstrate the issue.
Just make more concrete typing in your code, like this:
function someOf<T>(keys: (keyof T)[], obj: T): boolean {
return keys.some((key) => obj[key] > 0)
}
const res = someOf(['aaa', 'bbb'], myObject);