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 };
相关推荐
lbh1 天前
当我开始像写代码一样和AI对话,一切都变了
前端·openai·ai编程
We་ct1 天前
LeetCode 918. 环形子数组的最大和:两种解法详解
前端·数据结构·算法·leetcode·typescript·动态规划·取反
wefly20171 天前
m3u8live.cn 在线M3U8播放器,免安装高效验流排错
前端·后端·python·音视频·前端开发工具
C澒1 天前
微前端容器标准化 —— 公共能力篇:通用打印
前端·架构
德育处主任Pro1 天前
前端元素转图片,dom-to-image-more入门教程
前端·javascript·vue.js
木斯佳1 天前
前端八股文面经大全:小红书前端一二面OC(下)·(2026-03-17)·面经深度解析
前端·vue3·proxy·八股·响应式
陈天伟教授1 天前
人工智能应用- 预测新冠病毒传染性:04. 中国:强力措施遏制疫情
前端·人工智能·安全·xss·csrf
zayzy1 天前
前端八股总结
开发语言·前端·javascript
今天减肥吗1 天前
前端面试题
开发语言·前端·javascript
Rabbit_QL1 天前
【前端UI行话】前端 UI 术语速查表
前端·ui·状态模式