typescript 为什么类型Assert在这种特殊情况下不摆脱“元素隐式地具有'any'类型”?

bwleehnv  于 2023-08-07  发布在  TypeScript
关注(0)|答案(1)|浏览(112)

我有这样的代码(它有点捏造,但我需要它是这种方式和这种类型结构)。基本上,我希望数组以这种方式排序:

  • 首先按项目类型
  • 然后是主人
  • 然后按值

也就是说,如果有两个元素具有相同的itemType,则会考虑所有者,依此类推。这只是以防万一,它不应该与所讨论的tsc错误有任何直接关系。

'use strict';

type Item = {
  itemType: 'itemTypeA' | 'itemTypeB';
  belongsTo: 'ownerA' | 'ownerB';
  value: number;
};

type Argument = {
  items: Item[];
};

const a: Argument = {
  items: [
    {
      itemType: 'itemTypeB',
      belongsTo: 'ownerA',
      value: 0,
    },
    {
      itemType: 'itemTypeA',
      belongsTo: 'ownerB',
      value: 1,
    },
    {
      itemType: 'itemTypeB',
      belongsTo: 'ownerA',
      value: 10,
    },
    {
      itemType: 'itemTypeB',
      belongsTo: 'ownerA',
      value: 0,
    },
  ],
};
console.log(testFunc(a));

function testFunc(arg: Argument): string {
  const sortByData = new Map([
    ['itemType', { itemTypeA: 0, itemTypeB: 1, },],
    ['belongsTo', { ownerA: 0, ownerB: 1, },],
    ['value', null,],
  ]);
  arg.items.sort((a, b) => {
    for (const [propName, sortValues] of sortByData) {
      //// @ts-ignore: failed to make it work with type checks
      const aValue = sortValues ? sortValues[a[propName]] as any as number : a[propName];
      ////// @ts-ignore: failed to make it work with type checks
      //const bValue = sortValues ? sortValues[b[propName]] : b[propName];
      //if (aValue > bValue) {
      //  return 1;
      //} else if (aValue < bValue) {
      //  return -1;
      //}
    }
    return 0;
  });
  console.log('AAA', arg.items);
  return '123';
}

字符串
问题是这一行:

const aValue = sortValues ? sortValues[a[propName]] as any as number : a[propName];
                            ~~~~~~~~~~~~~~~~~~~~~~~


导致此错误:
元素隐式具有“any”类型,因为类型“any”的表达式不能用于索引类型“{ itemTypeA:number; itemTypeB:编号;所有者A?:never; ownerB?:never; }|{ ownerA:number; ownerB:number;项目类型A?:never; itemTypeB?:...
我知道这个(下划线的~~)表达式是一个数字。我只要Assert它是一个数字就足够了,但即使这样也不行。
那么,谁能解释一下,为什么类型Assert在这种情况下不起作用,或者更一般地说,什么是消 debugging 误的最佳方法?(我发现的唯一一个方法是禁用// @ts-ignore: failed to make it work with type checks的类型检查)

mbskvtky

mbskvtky1#

您在不同的位置有多个类型错误,因此需要多个类型Assert来解决它们。在像sortValues[a[propName]]这样的东西中,你依赖于propName被视为a的有效密钥,a[propName]被视为sortValues的有效密钥。对于您编写的代码,这两种情况都不成立。在这里讨论为什么会出现这种情况超出了范围,但是如果你想通过类型Assert来解决,你需要Assert其中的每一个:

const aValue = sortValues ?
    sortValues[a[propName as keyof Item] as keyof typeof sortValues] as number :
    a[propName as keyof Item];

字符串
如果你只是想通过关闭类型检查来解决错误,你可以通过使用any类型来减少Assert的数量:

arg.items.sort((a: any, b) => {
    for (const [propName, sortValues] of sortByData as Map<string, any>) {
        const aValue = sortValues ? sortValues[a[propName]] : a[propName];
    }
    return 0;
});


您可以使用//@ts-ignore注解指令来抑制错误,但除非您用尽了所有其他选项,否则我永远不会推荐您使用此方法。这样做不会关闭类型检查;它只是禁用 * 显示 * 错误。虽然错误可能会显示在代码的某行中,但潜在的问题可能并不局限于那一行。所以你可能会在代码的其他地方看到奇怪的问题。换句话说,编译器错误通常表示一些实际的问题,即使你//@ts-ignore它,实际的问题仍然存在。对于像类型Assert这样微不足道的东西,可能不会有这样的非局部效果,但是对于像类型Assert这样微不足道的东西,你应该首先使用类型Assert。正如文档中所说,“我们建议您非常谨慎地使用这些注解。”
Playground链接到代码

相关问题