为什么在类型定义中使用var是不可能的?

时间:2018-01-05 09:11:48

标签: typescript

type myType = 'A';
const myVar = 'A';

let a: 'A'; // OK
let b: myType; // OK
let c: myVar; // ERROR : cannot find name 'myVar'

为什么不能将var用作类型?应该不可能吗?

2 个答案:

答案 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是一个不是类型的变量,所以你不能使用变量作为类型。