0.s*_*.sh 11

简单的回答Yes

解释 (替换constlet

let b: string = 'test'我们明确表示我们想要b成为一个字符串,因此将字符串以外的任何内容分配给b将是一个类型错误。

let foo = 'hello' as string 使用as称为类型断言,我们告诉打字稿,我们想要foo成为这种特定类型

(对于您给出的示例,它们基本上做相同的事情),但是类型断言也可以在这样的情况下使用

interface User {
  name: string;
  age: number;
  occupation: Array<string>
}
// doing this will cause an error because the {} object does not 
// satisfy the contract defined in the User Interface (structurally)
let currentUser: User = {}; 

// in this case what we will do is use type assertion (typescript still does type checking as well
let currentUser: User = {} as User;

Run Code Online (Sandbox Code Playgroud)

从您的示例来看,两个代码都执行相同的操作,但是类型断言和显式指定变量的类型不同