面试题答案
一键面试1. 模块间传递数据方案
使用接口(Interfaces)来定义数据结构,通过导出接口和相关函数来进行模块间通信。例如:
// moduleA.ts
export interface User {
id: number;
name: string;
}
export function createUser(user: User): User {
return user;
}
// moduleB.ts
import { User, createUser } from './moduleA';
const newUser: User = { id: 1, name: 'John' };
const createdUser = createUser(newUser);
2. 利用TypeScript特性增强通信机制
- 类型别名(Type Aliases):可以为复杂类型创建简洁的别名,提高代码可读性。例如:
// moduleC.ts
export type UserId = number;
export type UserInfo = { name: string };
export type UserDetails = UserId & UserInfo;
export function getUserDetails(id: UserId, info: UserInfo): UserDetails {
return { id, ...info };
}
- 接口(Interfaces):不仅用于定义对象结构,还能用于定义函数类型,确保函数参数和返回值类型安全。如:
// moduleD.ts
export interface UserFetcher {
(id: number): Promise<User>;
}
export const fetchUser: UserFetcher = async (id) => {
// 模拟异步获取用户数据
return { id, name: 'Mock User' };
};
- 泛型(Generics):用于创建可复用的组件,在模块间传递不同类型数据时保持类型安全。例如:
// moduleE.ts
export function identity<T>(arg: T): T {
return arg;
}
// 使用泛型函数
const result = identity<string>('Hello');
3. 处理类型循环依赖问题
- 分离公共类型:将循环依赖涉及的公共类型提取到一个独立模块中,供依赖双方使用。例如:
// commonTypes.ts
export interface SharedType {
value: string;
}
// moduleX.ts
import { SharedType } from './commonTypes';
export interface ModuleXType {
shared: SharedType;
}
// moduleY.ts
import { SharedType } from './commonTypes';
export interface ModuleYType {
shared: SharedType;
}
- 使用类型导入(Type Imports):在TypeScript 3.8+版本,可以使用
import type
语法仅导入类型,避免实际的模块导入,减少循环依赖风险。例如:
// moduleA.ts
import type { ModuleBType } from './moduleB';
export function doSomethingWithB(typeB: ModuleBType) {
// 操作
}
// moduleB.ts
import type { ModuleAType } from './moduleA';
export function doSomethingWithA(typeA: ModuleAType) {
// 操作
}