ts笔记-声明空间

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'"
常用网站: SegmentFault | GitHub | 掘金社区
原文地址:https://www.cnblogs.com/yesyes/p/15394088.html