ts中存在两种声明空间:类型声明空间和变量声明空间

类型声明

类型声明空间用来做类型注释

interface Bar {}
type Bas = {};

let bar: Bar;
let bas: Bas;

// 但是不能当作变量使用
interface Bar {}
const bar = Bar; // Error: "cannot find name 'Bar'"

变量声明

变量声明空间除了可以做类型注释,可以当作变量使用

class Foo {}

// 当作类型使用
let foo: Foo;

// 当作变量使用
const someVar = Foo;
const someOtherVar = 123;

但要记住,不要把变量声明和变量空间声明搞混了

// 这是变量声明,不能用做类型注释
const foo = 123;
let bar: foo; // ERROR: "cannot find name 'foo'"

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-17
  • 2022-12-23
  • 2022-12-23
  • 2021-07-09
  • 2022-12-23
  • 2021-10-01
猜你喜欢
  • 2022-12-23
  • 2022-01-08
  • 2022-12-23
  • 2022-01-08
  • 2022-12-23
  • 2022-01-08
  • 2022-12-23
相关资源
相似解决方案