02.ts 280 B

12345678910111213141516
  1. let d;
  2. d = 10;
  3. d = "333";
  4. d = true;
  5. let s: string;
  6. s = d; //any值可以赋给其他不同类型
  7. let e: unknown;
  8. // s = e; unknown不能赋值其他
  9. if(typeof e === "string"){
  10. s = e; //方法1.先判断
  11. }
  12. s = e as string //方法2.类型断言
  13. s = <string>e //另外写法