TypeScript 已成为前端、全栈以及 Node.js 开发岗位的核心技能之一。2026 年的 TypeScript 面试不再停留在基础语法层面,而是更加关注类型系统、泛型设计、条件类型、类型推导以及 TypeScript 5.x 新特性等内容。下面整理了一份最新 TypeScript 高频面试题及参考答案,帮助开发者快速备战面试。部分新特性内容参考了 TypeScript 5.x 官方文档及社区最新实践。
18 个TypeScript面试题和答案汇总
1. TypeScript 与 JavaScript 的区别是什么?
TypeScript 是 JavaScript 的超集,在 JavaScript 的基础上增加了静态类型检查。
主要区别包括:
- 支持静态类型系统
- 支持接口 Interface
- 支持泛型 Generics
- 支持枚举 Enum
- 提供更好的 IDE 智能提示
- 编译阶段发现潜在错误
TypeScript 最终会编译成 JavaScript 运行在浏览器或 Node.js 环境中。
2. any、unknown、never 的区别是什么?
any:关闭类型检查。
let value: any = "hello";
value = 123;
value.toUpperCase();
unknown:类型安全版本的 any。
let value: unknown = "hello";
if (typeof value === "string") {
console.log(value.toUpperCase());
}
never:永远不会有值的类型。
function throwError(): never {
throw new Error("Error");
}
面试回答重点
- any 放弃类型检查
- unknown 必须先缩小类型范围
- never 表示不会正常返回
3. type 和 interface 有什么区别?
主要区别
| 特性 | interface | type |
|---|---|---|
| 对象定义 | √ | √ |
| 联合类型 | × | √ |
| 交叉类型 | × | √ |
| 声明合并 | √ | × |
| 元组定义 | × | √ |
面试建议
- 对象模型优先使用 interface。
- 联合类型、工具类型优先使用 type。
4. 什么是联合类型?
一个变量可以拥有多个类型。
let value: string | number;
value = "Hello";
value = 100;
应用场景:
function print(id: string | number) {
console.log(id);
}
5. 什么是交叉类型?
将多个类型合并为一个类型。
type User = {
name: string;
};
type Employee = {
salary: number;
};
type Staff = User & Employee;
结果:
{
name: string;
salary: number;
}
6. 什么是泛型?
泛型可以让函数或类支持多种类型。
function identity<T>(value: T): T {
return value;
}
调用:
identity<string>("hello");
identity<number>(123);
优势:
- 提高代码复用率
- 保持类型安全
7. extends 在泛型中的作用是什么?
用于约束泛型类型。
interface Length {
length: number;
}
function getLength<T extends Length>(arg: T) {
return arg.length;
}
允许:
getLength("hello");
getLength([1, 2, 3]);
禁止:
getLength(123);
8. Partial、Pick、Omit 的区别?
Partial:全部属性变为可选。
interface User {
id: number;
name: string;
}
type PartialUser = Partial<User>;
Pick:选择部分属性。
type UserInfo = Pick<User, "id" | "name">;
Omit:排除部分属性。
type UserDTO = Omit<User, "id">;
这些工具类型是企业面试中的高频考点。
9. 什么是条件类型?
根据条件返回不同类型。
type IsString<T> = T extends string ? true : false;
示例:
type A = IsString<string>;
type B = IsString<number>;
结果:
type A = true
type B = false
10. infer 关键字有什么作用?
用于条件类型中推断类型。
type ReturnType<T> =
T extends (...args: any[]) => infer R
? R
: never;
示例:
function getUser() {
return {
id: 1
};
}
type Result = ReturnType<typeof getUser>;
11. 什么是类型守卫(Type Guard)?
用于缩小类型范围。
function print(value: string | number) {
if (typeof value === "string") {
console.log(value.toUpperCase());
}
}
自定义守卫:
function isString(value: any): value is string {
return typeof value === "string";
}
Type Guard 在大型项目中应用非常广泛,也是高级岗位常考内容。
12. 什么是 Discriminated Union(可辨识联合类型)?
通过公共字段区分不同类型。
type Circle = {
kind: "circle";
radius: number;
};
type Rectangle = {
kind: "rectangle";
width: number;
height: number;
};
type Shape = Circle | Rectangle;
判断:
function area(shape: Shape) {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "rectangle":
return shape.width * shape.height;
}
}
13. TypeScript 5.x 的 satisfies 操作符是什么?
用于验证对象是否符合某个类型,同时保留更精确的推导结果。
type Config = {
port: number;
};
const config = {
port: 3000
} satisfies Config;
优势:
- 保留字面量类型
- 避免类型扩宽
- 增强类型安全
这是近两年面试中的热门新考点。
14. TypeScript 5.x 的 const Type Parameters 是什么?
用于在泛型中保留字面量类型。
function createRoutes<
const T extends readonly string[]
>(routes: T) {
return routes;
}
调用:
const routes = createRoutes([
"home",
"about"
]);
无需再使用大量 as const。
这是 TypeScript 5.x 最受关注的新特性之一。
15. TypeScript 5.x Decorators 有什么变化?
TypeScript 5.x 正式支持符合 TC39 标准的 Decorators。
function log(
originalMethod: any,
context: ClassMethodDecoratorContext
) {
return function (...args: any[]) {
console.log(args);
return originalMethod.call(this, ...args);
};
}
class UserService {
@log
getUser(id: string) {
return id;
}
}
面试官通常会关注:
- 装饰器原理
- 装饰器执行顺序
- NestJS 中的应用
16. TypeScript 如何实现函数重载?
function format(value: string): string;
function format(value: number): string;
function format(value: string | number) {
return value.toString();
}
调用:
format("hello");
format(100);
17. TypeScript 编译时类型和运行时类型有什么区别?
TypeScript 类型只存在于编译阶段。
例如:
let name: string = "Tom";
编译后:
let name = "Tom";
运行时不存在 string 类型信息。
因此:
- TypeScript 不能替代运行时校验
- 仍需配合 Zod、Joi 等方案
18. 如何提高大型 TypeScript 项目的性能?
常见优化方案:
- 开启 incremental
- 使用 Project References
- 开启 isolatedDeclarations
- 拆分 Monorepo
- 使用 Turborepo 或 Nx
这些也是近年高级开发岗位关注的重点。
面试总结
2026 年 TypeScript 面试主要集中在四个方向:
- 基础类型系统(type、interface、泛型)
- 高级类型编程(条件类型、infer、映射类型)
- 工具类型与工程实践(Partial、Pick、Omit)
- TypeScript 5.x 新特性(satisfies、const Type Parameters、Decorators)
对于中高级开发岗位来说,仅掌握基础语法已经远远不够。面试官更关注候选人是否能够利用 TypeScript 构建大型、可维护、高类型安全的项目架构,因此深入理解类型推导和高级类型设计能力将成为决定面试结果的重要因素。