te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - typescript: how to correctly type key and value parameters in Object.entries().forEach([key,value]) => - Sta
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - typescript: how to correctly type key and value parameters in Object.entries().forEach([key,value]) => - Sta

programmeradmin3浏览0评论

I got an object with specific key/value parameters and I want to iterate over it with entries() method of Object followed by a forEach() method of Array. However I do not understand how I have to type this configuration to avoiding a typescript error:

type objType = {
  prop1: number | undefined;
  prop2: number | undefined;
  prop3: number | undefined;
};

const obj: objType = {
  prop1: 2,
  prop2: 0,
  prop3: undefined,
};

//1st attempt
Object.entries(obj).forEach(([key, value]) => {
  if (value === undefined || value < 5) obj[key] = 5;
});

//2nd attempt
Object.entries(obj).forEach(
  ([key, value]: [keyof objType, number | undefined]) => {
    if (value === undefined || value < 5) obj[key] = 5;
  }
);

In first attempt, I let typescript infer the type of key (→ string) and value (→ number|undefined). But in this case I got an error when doing obj[key] :

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'objType'. No index signature with a parameter of type 'string' was found on type 'objType'.

In second attempt I forced the type of key to correspond to the key of obj using keyof operator, but this type definition is not allowed and I got the following message :

Argument of type '([key, value]: [keyof objType, number | undefined]) => void' is not assignable to parameter of type '(value: [string, number | undefined], index: number, array: [string, number | undefined][]) => void'. Types of parameters '__0' and 'value' are inpatible. Type '[string, number | undefined]' is not assignable to type '[keyof objType, number | undefined]'. Type at position 0 in source is not patible with type at position 0 in target. Type 'string' is not assignable to type 'keyof objType'.

I understand the failure for first attempt but not for the second. Why TS believes that I want to assign string to the string enumeration, I'm guessing to do the opposite...?

How is the correct way to type this configuration ?

I got an object with specific key/value parameters and I want to iterate over it with entries() method of Object followed by a forEach() method of Array. However I do not understand how I have to type this configuration to avoiding a typescript error:

type objType = {
  prop1: number | undefined;
  prop2: number | undefined;
  prop3: number | undefined;
};

const obj: objType = {
  prop1: 2,
  prop2: 0,
  prop3: undefined,
};

//1st attempt
Object.entries(obj).forEach(([key, value]) => {
  if (value === undefined || value < 5) obj[key] = 5;
});

//2nd attempt
Object.entries(obj).forEach(
  ([key, value]: [keyof objType, number | undefined]) => {
    if (value === undefined || value < 5) obj[key] = 5;
  }
);

In first attempt, I let typescript infer the type of key (→ string) and value (→ number|undefined). But in this case I got an error when doing obj[key] :

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'objType'. No index signature with a parameter of type 'string' was found on type 'objType'.

In second attempt I forced the type of key to correspond to the key of obj using keyof operator, but this type definition is not allowed and I got the following message :

Argument of type '([key, value]: [keyof objType, number | undefined]) => void' is not assignable to parameter of type '(value: [string, number | undefined], index: number, array: [string, number | undefined][]) => void'. Types of parameters '__0' and 'value' are inpatible. Type '[string, number | undefined]' is not assignable to type '[keyof objType, number | undefined]'. Type at position 0 in source is not patible with type at position 0 in target. Type 'string' is not assignable to type 'keyof objType'.

I understand the failure for first attempt but not for the second. Why TS believes that I want to assign string to the string enumeration, I'm guessing to do the opposite...?

How is the correct way to type this configuration ?

Share Improve this question asked Dec 20, 2021 at 10:14 Sebastien DSebastien D 831 gold badge1 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Why

First of all we need to understand why this happening: Let's check the simplest example:

const obj = {a: 1};
Object.keys(obj).forEach((key:keyof typeof obj)=> ''); // fails because Type 'string' is not assignable to type '"a"'

This happens, because TS can't guarantee that there is no other keys in the object. Thus, it can't guarantee that key will be ONLY 'a'.

In your case, this means that TS can't ensure that value of obj[key] will be number. It might be anything, thus TS throws an error.

Example

Option #1

One way to handle this is to force TS to use your types:

//1st attempt
Object.entries(obj).forEach(([key, value]) => {
  if (value === undefined || value < 5) obj[key as keyof typeof obj] = 5;
});

//2nd attempt
Object.entries(obj).forEach(
  ([key, value]) => {
    if (value === undefined || value < 5) obj[key as keyof typeof obj] = 5;
  }
);

This is OK in case you are absolute sure that no extra keys will be present.

Option #2

Another way is to redesign your types into something a bit more generic:

type objType = {
    prop1: number | undefined;
    prop2: number | undefined;
    prop3: number | undefined;
}

const obj: Record<string, number | undefined> = {
    prop1: 2,
    prop2: 0,
    prop3: undefined,
};

//1st attempt
Object.entries(obj).forEach(([key, value]) => {
    if (value === undefined || value < 5) obj[key] = 5;
});

//2nd attempt
Object.entries(obj).forEach(
    ([key, value]) => {
        if (value === undefined || value < 5) obj[key] = 5;
    }
);

At the code above we are clearly states that we allows any string keys.

Please check for some extra details

Option 3: Use a key signature

On your type objType add a key signature like so:

  [key: string]: number | undefined;

Here is the full code example:

type objType = {
  [key: string]: number | undefined;
  prop1: number | undefined;
  prop2: number | undefined;
  prop3: number | undefined;
};

const obj: objType = {
  prop1: 2,
  prop2: 0,
  prop3: undefined,
};

//1st attempt
Object.entries(obj).forEach(([key, value]) => {
  if (value === undefined || value < 5) obj[key] = 5;
});

//2nd attempt
Object.entries(obj).forEach(
  ([key, value]: [keyof objType, number | undefined]) => {
    if (value === undefined || value < 5) obj[key] = 5;
  }
);

Here is a screenshot showing VSCode is recognizing the typing:

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论