当前位置: 首页 > news >正文

面试题-ts中的typeof

在 TypeScript 里,typeof操作符在类型系统和 JavaScript 运行时中的表现有所不同。下面详细介绍它对基本类型、对象、数组和函数的返回结果:

一、TypeScript 类型系统中的 typeof

在类型注解、泛型约束等类型相关的上下文中,typeof用于获取变量或表达式的类型

1. 基本类型
const num: number = 42;
const str: string = "hello";
const bool: boolean = true;
const nul: null = null;
const undef: undefined = undefined;
const sym: symbol = Symbol();type NumType = typeof num;      // number
type StrType = typeof str;      // string
type BoolType = typeof bool;    // boolean
type NullType = typeof nul;     // null
type UndefType = typeof undef;  // undefined
type SymType = typeof sym;      // symbol

2. 对象

typescript

const person = {name: "Alice",age: 30,
};type PersonType = typeof person;
// 等同于:
// {
//   name: string;
//   age: number;
// }
3. 数组
const numbers = [1, 2, 3];
const mixed = [1, "a", true];type NumbersType = typeof numbers;      // number[]
type MixedType = typeof mixed;          // (number | string | boolean)[]
4. 函数
function add(a: number, b: number): number {return a + b;
}type AddFnType = typeof add;
// 等同于:
// (a: number, b: number) => number

二、JavaScript 运行时中的 typeof

在表达式中,typeof返回一个表示值类型的字符串(这和 TypeScript 类型系统不同)。

1. 基本类型

typeof 42;           // "number"
typeof "hello";      // "string"
typeof true;         // "boolean"
typeof null;         // "object"(JavaScript 历史遗留问题)
typeof undefined;    // "undefined"
typeof Symbol();     // "symbol"
2. 对象
typeof { name: "Alice" };  // "object"
typeof [1, 2, 3];          // "object"
typeof null;              // "object"(注意:null 不是对象!)
3. 函数
typeof function() {};     // "function"
typeof Math.sqrt;         // "function"

三、TypeScript 中 typeof 的常见应用

1. 提取已有变量的类型
const config = {apiKey: "secret",timeout: 5000,
};type ConfigType = typeof config;
// 等同于:
// {
//   apiKey: string;
//   timeout: number;
// }
2. 与 keyof 结合获取属性名联合类型
type ConfigKeys = keyof typeof config;  // "apiKey" | "timeout"
3. 泛型约束
function getProperty<T, K extends keyof T>(obj: T, key: K) {return obj[key];
}const timeout = getProperty(config, "timeout");  // number 类型

四、注意事项

  1. JavaScript 的 typeof null 问题

    javascript

    typeof null === "object";  // true(历史错误,无法修复)
    
  2. TypeScript 的 typeof 只能用于具体值

    type ErrorType = typeof number;  // 错误:不能直接对类型使用 typeof
    type CorrectType = typeof 42;    // 正确:对值使用 typeof
    
  3. 数组类型的特殊性

    const arr = [1, 2, 3];
    type ArrType = typeof arr;       // number[]
    type FirstElement = ArrType[0];  // number
    

总结

场景TypeScript 类型系统JavaScript 运行时
基本类型获取具体类型(如 numberstring返回字符串(如 "number"
对象获取对象结构类型返回 "object"
数组获取元素类型的数组(如 number[]返回 "object"
函数获取函数签名类型返回 "function"

合理运用 typeof 可以让你在 TypeScript 中更精准地进行类型定义和类型推导。

相关文章:

  • ACL 2025 Findings | 无监督概念发掘,提高文本解释的可理解性
  • 如何排查在docker中运行软件的故障:Docker故障排查可视化指南,三招锁定问题根源
  • 从0到1学Linux:Linux进程
  • BGP路由反射器(RR)实验详解,结尾有详细脚本
  • 【软考高级系统架构论文】论湖仓一体架构及其应用
  • ELB的设置
  • React 重识
  • Taro 跨端应用性能优化全攻略:从原理到实践
  • 基于强化学习的5G通信网络基站资源动态分配策略matlab性能仿真
  • 【软考高级系统架构论文】论面向服务架构设计及其应用
  • 【科研绘图系列】R语言绘制circos图形(circos plot)
  • 1. 常见K线组合
  • CertiK联创顾荣辉将于港大活动发表演讲,分享Web3安全与发展新视角
  • 【自动鼠标键盘控制器|支持图像识别】
  • 通达信 飞升搏杀大妖股飞升趋势指标
  • Electron 开发桌面应用概述
  • 贪心算法思路详解
  • 【设计模式】4.代理模式
  • 鸿蒙NEXT-鸿蒙三层架构搭建,嵌入HMRouter,实现便捷跳转,新手攻略。(1/3)
  • 黑马头条-数据管理平台
  • 杂志网站建设/百度网站域名注册
  • 合肥瑶海区/沈阳seo收费
  • 大连网站建设怎么样/太原网站制作优化seo公司
  • 网站优化建设宁夏/seo关键词优化公司哪家好
  • 政府网站域名要求/关键词搜索工具有哪些
  • wordpress建壁纸站/广告联盟app下载官网