如何在 TypeScript 中使用 `some` 部分检查对象中的项目

huangapple go评论61阅读模式
英文:

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 = [&#39;aaa&#39;, &#39;bbb&#39;].some((key) =&gt; myObject[key] &gt; 0)

but for myObject[key] I'm getting a TypeScript error about:

Element implicitly has an &#39;any&#39; type because expression of type &#39;string&#39; can&#39;t be used to index type &#39;INxStateCounts&#39;.
  No index signature with a parameter of type &#39;string&#39; was found on type &#39;INxStateCounts&#39;.

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&lt;T&gt;(keys: (keyof T)[], obj: T): boolean {
  return keys.some((key) =&gt; obj[key] &gt; 0)
}

const res = someOf([&#39;aaa&#39;, &#39;bbb&#39;], 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&lt;keyof MYInterface&gt; = [&#39;aaa&#39;, &#39;bbb&#39;]

const res = keys.some((key) =&gt; (myObject[key] !== undefined &amp;&amp; myObject[key]! &gt; 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 &#39;aaa&#39;|&#39;bbb&#39; instaed of string/any.

const res = ([&#39;aaa&#39;, &#39;bbb&#39;] as const).some((key) =&gt; myObject[key] &gt; 0)

huangapple
  • 本文由 发表于 2023年2月8日 19:13:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定