当返回任何as类型时,为什么TypeScript编译器不会抱怨?

时间:2020-05-01 10:13:55

标签: typescript

如果我的函数返回类型any而不是声明的返回类型,我希望TypeScript编译器抱怨。

这可能吗?

function myFunction():number{
  return 'a string that is not a number' as any;
}

//  ↑ I would like to throw an error in the compiler

这里的问题是,此函数将按预期返回字符串而不是数字。

我想在其他功能中使用类型any,所以我不想禁用类型any

编辑

这只是一个例子。我的真正问题是我正在使用某些返回类型为any的库。因此,实现过程不会抱怨。

function myFunction():number{
  return anotherFunctionThatHasReturnTypeAny();
}

//  ↑ I would like to throw an error in the compiler

如果返回类型为any,是否有可能使编译器失败?

0 个答案:

没有答案