英文:
How to use `some` to partially check items in object using TypeScript
问题
我有一个对象:
```tsx
interface MYInterface {
aaa: number;
bbb: number;
ccc?: number | undefined;
}
const myObject: MYInterface = {
aaa: 0,
bbb: 0,
ccc: 132,
};
我想检查这个对象中的一些键是否满足某个条件!我正在使用类似以下的 Array.some(...)
:
const res = ['aaa', 'bbb'].some((key) => myObject[key] > 0)
但是对于 myObject[key]
,我收到了有关 TypeScript 的错误:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'MYInterface'.
No index signature with a parameter of type 'string' was found on type 'MYInterface'.
我知道我试图做的是使用对象中的字符串键数组,但我的数组包含这些字符串键。我尝试将键强制转换为 keyof MYInterface
,但没有成功!我遇到了很多其他错误。我该如何解决这个问题?
此外,我的对象非常庞大,在这里我仅使用3个属性来演示问题。
<details>
<summary>英文:</summary>
I have an object:
```tsx
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 in case if you want to test.
答案1
得分: 3
以下是您要的翻译:
只需在您的代码中使类型更具体,就像这样:
function someOf<T>(keys: (keyof T)[], obj: T): boolean {
return keys.some((key) => obj[key] > 0)
}
const res = someOf(['aaa', 'bbb'], myObject);
英文:
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);
答案2
得分: 1
你可以像这样写:
```ts
const keys: Array<MYInterface的键类型> = ['aaa', 'bbb'];
const res = keys.some((key) => (myObject[key] !== undefined && myObject[key] > 0));
由于进行了undefined
检查,某种原因需要使用感叹号!
运算符。
<details>
<summary>英文:</summary>
You can write something like this:
```ts
const keys : Array<keyof MYInterface> = ['aaa', 'bbb']
const res = keys.some((key) => (myObject[key] !== undefined && myObject[key]! > 0))
for some reason the bang !
operator is needed despite undefined
checking.
答案3
得分: 0
如果您使用对象索引签名,您可能会得到您想要的结果。尝试:
interface MYInterface {
[key: string]: number;
}
其他处理此问题的方法:如何在TypeScript中动态分配对象属性
英文:
If you use object index signature you might get what you are looking for. Try:
interface MYInterface {
[key:string]: number;
}
...
Other ways to approach this: How to dynamically assign properties to an object in TypeScript
答案4
得分: 0
你可以添加 as const
以将其转换为元组类型。然后,key
参数的类型将变为 'aaa'|'bbb'
,而不是字符串/任意类型。
const res = (['aaa', 'bbb'] as const).some((key) => myObject[key] > 0)
英文:
You can add as const
to make it a tuple type. Then, the type of key
argument will become 'aaa'|'bbb'
instaed of string/any.
const res = (['aaa', 'bbb'] as const).some((key) => myObject[key] > 0)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论