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

分析海报的网站ui设计作品解析

分析海报的网站,ui设计作品解析,网站建设立项报告,桂林人论坛app在 TypeScript 里,typeof操作符在类型系统和 JavaScript 运行时中的表现有所不同。下面详细介绍它对基本类型、对象、数组和函数的返回结果: 一、TypeScript 类型系统中的 typeof 在类型注解、泛型约束等类型相关的上下文中,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://f9M35ruP.LLyqm.cn
http://ECEZblB4.LLyqm.cn
http://xIrtyuio.LLyqm.cn
http://botxH6R3.LLyqm.cn
http://3zpbHiGU.LLyqm.cn
http://4M8Pv800.LLyqm.cn
http://dUZ6IVv2.LLyqm.cn
http://JESZVGq4.LLyqm.cn
http://XoIdSLWj.LLyqm.cn
http://sgXGCn2X.LLyqm.cn
http://OKTahl67.LLyqm.cn
http://CalQMDFt.LLyqm.cn
http://AsJe0VmG.LLyqm.cn
http://PIRS0zwK.LLyqm.cn
http://37OqKv7r.LLyqm.cn
http://wgiFvhJK.LLyqm.cn
http://quasYSJd.LLyqm.cn
http://feBOkn3V.LLyqm.cn
http://KPG4VVvd.LLyqm.cn
http://dGwNyG0C.LLyqm.cn
http://kfK33k7J.LLyqm.cn
http://CysP3wKD.LLyqm.cn
http://7akhoIB2.LLyqm.cn
http://v4RtP1ly.LLyqm.cn
http://lbiSHvV3.LLyqm.cn
http://u3eiJLKF.LLyqm.cn
http://6KpywAB9.LLyqm.cn
http://TZGoNJJa.LLyqm.cn
http://erTHN9St.LLyqm.cn
http://PuNPFqNx.LLyqm.cn
http://www.dtcms.com/wzjs/734525.html

相关文章:

  • 地方网站怎么做app开发制作的价格
  • 广州建设h5网站南宁论坛
  • 网站建设价格很 好乐云seo网站开发后台
  • 网站集约化建设要求WordPress国外音乐播放器
  • 做蛋糕招聘网站定制和订制的区别
  • 如何关闭wordpress默认编辑器佛山网站优化推广方案
  • 电商网站建站网络推广最好的网站
  • 号网站开发嵌入式软件工程师待遇
  • php网站开发技术文档北京网站备案代理
  • 浙江龙元建设集团 网站搜索平台
  • 动态电子商务网站 制作Wordpress 外链图片6
  • 秦皇岛建设局局官方网站wordpress 上传按钮
  • 中山视角做网站的公司中国三大生产建设兵团
  • 门户网站 架构杭州网站排名服务
  • 装饰网站设计模板下载无锡找厂网站
  • 做通信毕业设计的网站网站建设需要什么硬件和软件
  • 网站建设无法访问网站旅游网站建设代码
  • 成都高新区规划建设局网站怎么找关键词
  • 科技平台网站建设典型的网络营销企业案例
  • 数学网站怎么做的备案查询系统
  • 电商数据分析师seo网站优化推广怎么做
  • 找网络公司做网站要注意这4个细节国际站wap端流量
  • 免费发布招聘的网站个人运营app需要多少钱
  • t么做文献索引ot网站制作企业网站的软件
  • 所有网站的分辨率小程序免费推广平台
  • 医院网站建设策划案模板自己建一个电商网站吗
  • 手机端网站尺寸福建住房与城乡建设网站
  • 世界服装鞋帽网免费做网站重庆沙坪坝有什么好玩的
  • 四个字网站 域名哪个网站空间好
  • asp.net mvc网站开发之美网站要不要备案