当前位置: 首页 > 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.lryc.cn/news/573410.html

相关文章:

  • 面试题-把类型为b的值赋给类型为a的变量
  • Laravel 项目中图片上传后无法访问的问题
  • SQL关键字三分钟入门:INSERT INTO —— 插入数据详解
  • Python实现MySQL建表语句转换成Clickhouse SQL
  • 【格与代数系统】偏序关系、偏序集与全序集
  • 2048小游戏C++板来啦!
  • 【Docker基础】Docker镜像管理:docker rmi、prune详解
  • 竞业限制协议能单独充当商业秘密的 “保护伞” 吗?
  • docker执行yum报错Could not resolve host: mirrorlist.centos.org
  • python web开发-Flask 蓝图(Blueprints)完全指南
  • 【Docker 08】Compose - 容器编排
  • C#测试调用EPPlus根据批注设置excel单元格内容
  • JavaEE初阶第三期:解锁多线程,从 “单车道” 到 “高速公路” 的编程升级(一)
  • 【开源项目】当大模型推理遇上“性能刺客”:LMCache 实测手记
  • linux安装minio并使用
  • 在Docker、KVM、K8S常见主要命令以及在Centos7.9中部署的关键步骤学习备存
  • XCUITest + Objective-C 详细示例
  • FastGPT:开启大模型应用新时代(4/6)
  • Springboot 配置 FastJson 替换 Jackson
  • Rabbitmq集成springboot,手动确认消息basicAck、basicNack、basicReject的使用
  • 在 MyBatis 的xml中,什么时候大于号和小于号可以不用转义
  • Axios 在 Vue3 项目中的使用:从安装到组件中的使用
  • 升级到 .NET 9 分步指南
  • “最浅”的陷阱:聊聊二叉树最小深度的递归坑点与解法哲学
  • 秋招Day14 - MySQL - SQL优化
  • c++11标准(5)——并发库(互斥锁)
  • 一、什么是生成式人工智能
  • 终端里的AI黑魔法:OpenCode深度体验与架构揭秘
  • Java ArrayList集合和HashSet集合详解
  • 【论文笔记】【强化微调】TinyLLaVA-Video-R1:小参数模型也能视频推理