type myType = 'A';
const myVar = 'A';
let a: 'A'; // OK
let b: myType; // OK
let c: myVar; // ERROR : cannot find name 'myVar'
为什么不能将var用作类型?应该不可能吗?
答案 0 :(得分:2)
Comparator<Row> comparatorRow = Comparator.comparing(Row::isLine).reversed();
是常量,您不能将变量键入为常量,您可以指定变量与常量具有相同的类型:
myVar
请注意,由于常量为“A”,因此上述声明与
相同let c: typeof myVar;
因此唯一可分配的值tp let c: "A";
是常量值c
答案 1 :(得分:2)
在TypeScript
中有四种方法来对del变量进行delcare var, let, const & Type (Type will be custom datatype)
let & var
非常相似,而const
则阻止重新分配给变量。你可以在var&amp; amp;让here。
const
是不变的,它在TypeScript中的行为与C#相同。在C#中,我们也不能使用const作为类型。
主要回答你的问题 - myVar是一个不是类型的变量,所以你不能使用变量作为类型。