TypeScript00:基础数据类型

TS 是 JS 的超集

JavaScript 与 TypeScript 的关系

TS 数据类型

数值

ts 复制代码
let age: number = 30;

字符串

ts 复制代码
let name: string = "Alice";

布尔值

ts 复制代码
let isDone: boolean = true;

array 数组

可以使用 type[] 或 Array<type> 两种方式表示

ts 复制代码
let list: [] = [1, "hello", 3];

let list: number[] = [1, "hello", 3]; //Type 'string' is not assignable to type 'number'.

let names: Array<string> = ["Alice", "Bob"];

tuple 元组

表示已知数量和类型的数组。

每个元素可以是不同的类型,适合表示固定结构的数据。

ts 复制代码
let person: [string, number] = ["Alice", 25];

let a: [string, number] = ["Alice", 25, 1]; //Type '[string, number, number]' is not assignable to type '[string, number]'.Source has 3 element(s) but target allows only 2.

let b: [string, number] = [2, 25]; //Type 'number' is not assignable to type 'string'.

enum 枚举

用来定义一组命名常量。

默认情况下枚举的值从 0 开始递增。

自定义枚举值会影响后续值,字符串枚举必须为所有成员显式赋值

ts 复制代码
enum Color {
  Red,
  Green,
  Blue,
}

let favoriteColor: Color = Color.Green;
console.log(favoriteColor); // 1

console.log(Color.Red); // 0
console.log(Color[1]); // Green
console.log(Color.Blue); // 2

enum Color1 {
  Red,
  Green = 2,
  Yellow = 100,
  Blue,
}

console.log(Color1.Red); // 0
console.log(Color1.Blue); // 101

enum Direction {
  Up = "UP",
  Down, //Enum member must have initializer.
  Left = 1,
}

void 空类型

用于没有返回值的函数。

声明变量时,类型 void 意味着只能赋值 null 或 undefined。

ts 复制代码
function logMessage(message: string): void {
  console.log(message);
}

logMessage("Hello, TypeScript!"); //Hello, TypeScript!

let x: void = 1; //Type 'number' is not assignable to type 'void'.

null 和 undefined

  • null 表示 "空值或空引用"。null 只有一个值,即 null。typeof 检测 null 返回 object。

  • undefined 表示 "未设置值"。undefined 只有一个值,即 undefined。typeof 一个没有值的变量会返回 undefined。undefined 不是"未定义变量",undefined 是已声明但未赋值的变量默认值,使用"未定义变量"会报错

在默认情况下,它们是其他类型(不包括 never)的子类型,可以赋值给其它类型

ts 复制代码
let x: number;
x = 1;
x = undefined;
x = null;
console.log(x); // null

never 类型

表示不会有返回值,通常用于抛出错误或进入无限循环的函数,表示该函数永远不会正常结束

ts 复制代码
function throwError(message: string): never {
  throw new Error(message);
}

never 是所有类型(包括 null 和 undefined)的子类型

ts 复制代码
let x: null;
x = never; //Type 'never' is not assignable to type 'null'.

any 类型

any 是顶级类型,表示任意类型,任何值都可以赋值给 any,会忽略类型检查

ts 复制代码
let randomValue1: any = 42;
randomValue = "hello";
console.log(randomValue); // hello

let randomValue1: number = 42;
randomValue1 = "hello"; //Type 'string' is not assignable to type 'number'.

unknown 不确定的类型

与 any 类似,但更严格。必须经过类型检查后才能赋值给其他类型变量。

ts 复制代码
let value: unknown = "Hello";

if (typeof value === "string") {
  let message: string = value;
}

object 对象类型

ts 复制代码
let person: object = { name: "Alice", age: 30 };
相关推荐
iphone1087 分钟前
一次编码,多端运行:HTML5多终端调用
前端·javascript·html·html5
老坛00124 分钟前
2025决策延迟的椭圆算子分析:锐减协同工具的谱间隙优化
前端
老坛00125 分钟前
从记录到预测:2025新一代预算工具如何通过AI实现前瞻性资金管理
前端
今禾28 分钟前
" 当Base64遇上Blob,图像转换不再神秘,让你的网页瞬间变身魔法画布! "
前端·数据可视化
华科云商xiao徐32 分钟前
高性能小型爬虫语言与代码示例
前端·爬虫
十盒半价33 分钟前
深入理解 React useEffect:从基础到实战的全攻略
前端·react.js·trae
攀登的牵牛花34 分钟前
Electron+Vue+Python全栈项目打包实战指南
前端·electron·全栈
iccb101334 分钟前
我是如何实现在线客服系统的极致稳定性与安全性的
前端·javascript·后端
一大树35 分钟前
Vue3祖孙组件通信方法总结
前端·vue.js
不要进入那温驯的良夜36 分钟前
跨平台UI自动化-Appium
前端