当前位置: 首页 > 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 中更精准地进行类型定义和类型推导。

http://www.lqws.cn/news/462997.html

相关文章:

  • 读者写者问题与读写锁自旋锁
  • OpenAI与微软的未来合作之路:充满挑战的AI竞赛与共赢
  • STM32F103C8T6 学习笔记摘要(二)
  • Knife4j 使用详解
  • (详细介绍)线性代数中的零空间(Null Space)
  • GitHub Copilot快捷键
  • JVM(8)——详解分代收集算法
  • linux生产环境下根据关键字搜索指定日志文件命令
  • Android多进程数据共享:SharedPreferences替代方案详解
  • RocketMQ--为什么性能不如Kafka?
  • 黑马头条-数据管理平台
  • Codeforces Round 1028 (Div. 2) A-C
  • ByteMD Markdown编辑器详细解释修改编辑器默认样式(高度300px)
  • Sublime text启用vim
  • 力扣刷题(第六十四天)
  • 咨询大师——解读96页麦肯锡金字塔原理【附全文阅读】
  • Qt输入数据验证的方法
  • 服务器架构---三高是什么
  • Ruby 范围(Range)
  • 如何用 eBPF 实现 Kubernetes 网络可观测性?实战指南
  • DM8故障分析工具-AWR报告
  • PY32学习(2)-搭建Keil环境
  • 基于SpringBoot+Uniapp的活动中心预约小程序(协同过滤算法、腾讯地图、二维码识别)
  • Linux 内核中 TCP 协议栈的输出实现:tcp_output.c 文件解析
  • 蓝牙数据通讯,实现内网电脑访问外网电脑
  • 针对机器人自修复材料的具体推荐及特性分析
  • STM32 CAN简介及帧格式
  • 操作系统内核态和用户态--1-基础认识
  • [Github]GitHub 2FA快速安全配置全攻略
  • 解决SQL映射文件的警告提示