简体   繁体   English

打字稿泛型:“T”和“数字”类型没有重叠错误

[英]Typescript generics: types 'T' and 'number' have no overlap error

Consider this example:考虑这个例子:

function foo<T>(t: T): boolean {
  return t === 1;
}

I get This condition will always return 'false' since the types 'T' and 'number' have no overlap.我得到This condition will always return 'false' since the types 'T' and 'number' have no overlap. . .

The error goes away if I change it to:如果我将其更改为:

function foo<T extends any>(t: T): boolean {
  return t === 1;
}

I thought T could be any by default, so why did I need extends any ?我认为默认情况下T可以是any ,那么为什么我需要extends any Without extends any , what type does TS expect T to be?没有extends any ,TS 期望T是什么类型?

Edit, actual function:编辑,实际功能:

function cleanObj<T>(
  obj: ObjectOf<T>,
): ObjectOf<T> {
  const newObj = {};
  for (const k of Object.keys(obj)) {
    if (obj[k] !== null && typeof obj[k] !== 'undefined' && obj[k] !== false) {
      newObj[k] = obj[k];
    }
  }

  return newObj;
}

The triple equals operator === returns true if both operands are of the same type and contain the same value.如果两个操作数的类型相同包含相同的值,则三元等号运算符===返回 true。 Compared to that "1" == 1 would return true.相比之下, "1" == 1将返回 true。

Since you use === you also compare the type, which is number for the right hand operant 1 .由于您使用===您也比较类型,这是number换右手操作性1 However, your T can not just be a number, that is why the compiler gives you this notification.然而,你的 T 不能只是一个数字,这就是编译器给你这个通知的原因。

Possible solutions:可能的解决方案:

  • remove the generics if not really necessary如果不是真的有必要,请删除泛型
  • use == instead of ===使用==而不是===
  • (possible further approaches depending on your actual code) (可能的进一步方法取决于您的实际代码)

Partially taken from https://github.com/microsoft/TypeScript/issues/17445 :部分摘自https://github.com/microsoft/TypeScript/issues/17445

Consider your original function:考虑您的原始功能:

function compare<T>(obj: T): boolean {
  return obj === 1;
}

if it would be allowed to compare anything to T we could also write:如果允许将任何内容与T进行比较,我们也可以这样写:

function compare<T>(obj: T): boolean {
  return obj === 'some-string-thats-definitely-not-obj';
}

If you use that function inside of some if , you could potentially create unreachable code that won't throw an error.如果在 some if使用该函数, if可能会创建不会引发错误的无法访问的代码。

This is why typescript requires you to explicitely tell it that T can be any , which is obviously unsafe for the above reason.这就是为什么打字稿要求您明确告诉它T可以是any ,由于上述原因,这显然是不安全的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript 错误 此条件将始终返回“真”,因为类型没有重叠 - Typescript error This condition will always return 'true' since the types have no overlap Typescript:此条件将始终返回“false”,因为类型“Type”和“number”没有重叠 - Typescript: This condition will always return 'false' since the types 'Type' and 'number' have no overlap Typescript 错误条件将始终返回“真”,因为“字符串”和“0”类型没有重叠 - Typescript error condition will always return 'true' since the types 'string' and '0' have no overlap 错误 - ''此条件将始终返回 false,因为类型 ''string'' 和 ''number'' 没有重叠'' - Error - ''This condition will always return false since the types ''string'' and ''number'' have no overlap'' Typescript 说:“类型“a”和“b”没有重叠”。 这是什么意思? - Typescript says: "types '"a"' and '"b"' have no overlap". What does it mean? 错误 - 此条件将始终返回“false”,因为类型““$value””和“”“”没有重叠 - Error - This condition will always return 'false' since the types '"$value"' and '""' have no overlap 在 TypeScript 映射类型中保留泛型 - Preserving generics in TypeScript mapped types 打字稿中的泛型-未定义T - Generics in Typescript - Undefined T 具有不同类型数量的打字稿接口 - typescript interface with different number of types TypeScript:函数重载不适用于泛型 - TypeScript: function overload doesn't work with generics
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM