Typescript 断言类型不是任何类型

Typescript 断言类型不是任何类型,typescript,Typescript,如何测试/断言变量类型在Typescript中不是“any” 我不需要运行时检查,如果编译器检测到以下错误就足够了: function notAny(x: (rejects type any)) { ...} let foo: any = 'value' // tsc should throw an error here notAny(foo) 解决方案应该只进行一次检查,而不是整个项目(如使用--noImplicitAny)下面的代码应该可以工作。基本思想是,any是唯一同时是unknow

如何测试/断言变量类型在Typescript中不是“any”

我不需要运行时检查,如果编译器检测到以下错误就足够了:

function notAny(x: (rejects type any)) { ...}
let foo: any = 'value'

// tsc should throw an error here
notAny(foo)

解决方案应该只进行一次检查,而不是整个项目(如使用
--noImplicitAny

下面的代码应该可以工作。基本思想是,
any
是唯一同时是
unknown
{}
底部(空)类型的基本类型

输入IsAny=
未知的T?T扩展{}?T:从来没有,从来没有;
类型符号=
艾萨尼?从不:T;
函数notAny(x:notAny){}
让foo:any='value'
不(foo);//错误如预期

{}
不是底部类型;它更接近top类型(在引入
unknown
之前,它是TS中的非官方top类型)。也许你的意思是,
any
是唯一被视为顶部类型(
T extends any
对所有
T
)和底部类型(
any extends T
对所有
T
)的类型?