字面量类型

Tutorial: TS初级一 Category: TS Published: 2026-04-07 13:58:26 Views: 20 Likes: 0 Comments: 0
  1. 字面量类型
let str1: string = "hi";

const str2 = "hi";

let age: 18 = 18;

function changeDirection(dir: "left" | "right" | "up" | "down") {
  console.log(dir);
}

// 只能传上面四个参数
changeDirection("left");
Prev: 类型推断 Next: 枚举