在这种特殊情况下,类型断言为什么不摆脱“元素隐式具有‘任何’类型”?

问题描述 投票:0回答:1

我有这段代码(它有点编造,但我需要它是这样的,并且具有这种类型结构)。 基本上我想要一个数组以这种方式排序:

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

即如果有两个元素具有相同的 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?:从不;所有者B?:从不; } | { 所有者A:号码;所有者B:号码; itemTypeA?:从不; itemTypeB?:…

我知道这个(带下划线的~~)表达式是一个数字。对我来说,只要断言它是一个数字就足够了,但即使这样也行不通。

有人可以解释一下,为什么类型断言在这种情况下不起作用,或者更一般地说,消除错误的最佳方法是什么?(我发现的唯一方法就是禁用使用

// @ts-ignore: failed to make it work with type checks
)

进行类型检查
javascript typescript typeerror tsc typechecking
1个回答
1
投票

您在不同位置有多个类型错误,因此您需要多个类型断言来解决它们。在类似

sortValues[a[propName]]
的情况下,您依赖
propName
被视为
a
的有效密钥,并且
a[propName]
被视为
sortValues
的有效密钥。对于您编写的代码来说,这些都不是正确的。探究为什么会出现这种情况超出了本文的范围,但如果您想通过类型断言来解决,则需要断言其中的每一个:

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

如果您只是尝试通过关闭类型检查来解决错误,则可以通过使用

any
类型:

来减少断言数量
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它,实际问题仍然存在。对于像类型断言这样微不足道的东西,可能不会有这样的非局部效果,但是对于像类型断言这样微不足道的东西,你应该首先使用类型断言。正如文档中所述,“我们建议您
非常谨慎地使用这些注释。”
Playground 代码链接

© www.soinside.com 2019 - 2024. All rights reserved.