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

微信看视频打赏网站建设新闻源发稿平台

微信看视频打赏网站建设,新闻源发稿平台,自我介绍ppt插图,建网站找哪家公司type interface总是傻傻分不清~~~ Type Aliases (type) type 关键字用于为类型定义一个别名。这可以是基本类型、联合类型、元组、数组、函数等。type 定义的类型在编译后的 JavaScript 代码中会被移除,不会留下任何运行时的代码。 //联合类型 type StringOrNumbe…

type interface总是傻傻分不清~~~

Type Aliases (type)

type 关键字用于为类型定义一个别名。这可以是基本类型、联合类型、元组、数组、函数等。type 定义的类型在编译后的 JavaScript 代码中会被移除,不会留下任何运行时的代码。

//联合类型
type StringOrNumber = string | number;
type StringOrNumber = 1 | 2;//数组类型
type Point = number[];//元组类型
type Point = [number, number];//函数类型
type Greeter = (name: string) => void;const myPoint: Point = [10, 20];
const greet: Greeter = function(name) {console.log("Hello, " + name);
};

Interfaces (interface)

interface 关键字用于定义对象的形状或类的公共结构。它可以包含方法签名、属性和索引签名。接口通常用于对类进行类型检查,确保类实现了接口中定义的所有成员。

interface Person {firstName: string;lastName: string;age?: number; // 可选属性
}const person: Person = {firstName: "John",lastName: "Doe",age: 30
};
异同

相同

  1. type和interface都可以用来定义对象和函数

  2. 都可以实现继承

不同

  1. type 可以声明基本类型、联合类型、元组类型、通过typeof 操作符来声明
  2. interface 可以声明合并。
使用场景

1、官方推荐使用 interface,其他无法满足需求的情况下用 type。但是因为联合类型和交叉类型是比较常用的,所以避免不了大量使用 type 的场景,一些复杂类型也需要通过组装后形成类型别名来使用。

2、如果想保持代码统一,还是可选择使用 type。通过上面的对比,type 其实可涵盖 interface 的大部分场景。

3、对于 React 组件中 props 及 state,推荐使用 type,这样能够保证使用组件的地方不能随意在上面添加属性。如果有自定义需求,可通过 HOC(高阶组件)二次封装。

4、编写三方库时使推荐使用 interface,其更加灵活自动的类型合并可应对未知的复杂使用场景。

Enums (enum)

enum 关键字用于定义枚举类型,它是一种特殊的类型,用于定义一组命名的常数。枚举成员被赋值为数字,从 0 开始递增,除非显式地指定一个值。

enum Color {Red,Green,Blue
}const c: Color = Color.Green;
1.数值枚举

在数值枚举中,每个成员默认从 0 开始自动赋值,并且每个成员的值依次递增 1

enum Color {Red,Green,Blue
}// 使用枚举
const favoriteColor: Color = Color.Green;
console.log(favoriteColor); // 输出: 2
console.log(Color[2]); // 输出: "Green"

如果你想要手动指定枚举成员的值,可以这样做:

enum Color {Red = 1,Green = 2,Blue = 3
}const favoriteColor: Color = Color.Green;
console.log(favoriteColor); // 输出: 2
2.字符串枚举

字符串枚举使用花括号 {} 定义,并且每个成员必须显式地指定一个字符串值。

enum Color {Red = "red",Green = "green",Blue = "blue"
}const favoriteColor: Color = Color.Green;
console.log(favoriteColor); // 输出: "green"
console.log(Color["Green"]); // 输出: "green"
3.反向映射

在 TypeScript 的枚举中,你可以通过枚举类型本身来访问枚举成员的名称,这称为反向映射。这在调试时非常有用,因为它允许你通过值快速找到对应的枚举名称。

enum Color {Red,Green,Blue
}console.log(Color[0]); // 输出: "Red"
console.log(Color[1]); // 输出: "Green"
console.log(Color[2]); // 输出: "Blue"

文章转载自:
http://roderick.hqbk.cn
http://pastiness.hqbk.cn
http://repairable.hqbk.cn
http://offenseless.hqbk.cn
http://netop.hqbk.cn
http://arenaceous.hqbk.cn
http://predominance.hqbk.cn
http://macrochemistry.hqbk.cn
http://patience.hqbk.cn
http://hypodynamic.hqbk.cn
http://fanfaron.hqbk.cn
http://infelicity.hqbk.cn
http://mobilize.hqbk.cn
http://lives.hqbk.cn
http://rework.hqbk.cn
http://outdistance.hqbk.cn
http://subjunctive.hqbk.cn
http://clinostat.hqbk.cn
http://circumsolar.hqbk.cn
http://hello.hqbk.cn
http://speciality.hqbk.cn
http://financier.hqbk.cn
http://curvirostral.hqbk.cn
http://thermometry.hqbk.cn
http://crus.hqbk.cn
http://antimony.hqbk.cn
http://emplacement.hqbk.cn
http://ins.hqbk.cn
http://oligomer.hqbk.cn
http://breastpin.hqbk.cn
http://orbiter.hqbk.cn
http://duke.hqbk.cn
http://vitrify.hqbk.cn
http://abhorrer.hqbk.cn
http://intercross.hqbk.cn
http://subarea.hqbk.cn
http://messina.hqbk.cn
http://catadioptrics.hqbk.cn
http://dextrine.hqbk.cn
http://oversimplify.hqbk.cn
http://king.hqbk.cn
http://cantaloup.hqbk.cn
http://superweak.hqbk.cn
http://pyorrhoea.hqbk.cn
http://acclivitous.hqbk.cn
http://brazenfaced.hqbk.cn
http://conspiratress.hqbk.cn
http://tympan.hqbk.cn
http://sandek.hqbk.cn
http://mac.hqbk.cn
http://subpopulation.hqbk.cn
http://official.hqbk.cn
http://zamboanga.hqbk.cn
http://shantey.hqbk.cn
http://barnaby.hqbk.cn
http://valid.hqbk.cn
http://coedit.hqbk.cn
http://tegucigalpa.hqbk.cn
http://changchun.hqbk.cn
http://soothingly.hqbk.cn
http://fortis.hqbk.cn
http://demibastion.hqbk.cn
http://prebiologic.hqbk.cn
http://cisrhenane.hqbk.cn
http://vigneron.hqbk.cn
http://lazaretto.hqbk.cn
http://hypnotism.hqbk.cn
http://sumpitan.hqbk.cn
http://spessartite.hqbk.cn
http://trichocarpous.hqbk.cn
http://jollop.hqbk.cn
http://microinterrupt.hqbk.cn
http://agronome.hqbk.cn
http://contrabass.hqbk.cn
http://chlamydomonas.hqbk.cn
http://hexane.hqbk.cn
http://slag.hqbk.cn
http://sketchpad.hqbk.cn
http://hypertensive.hqbk.cn
http://quadraminium.hqbk.cn
http://malinowskian.hqbk.cn
http://impersonify.hqbk.cn
http://plaintful.hqbk.cn
http://desmitis.hqbk.cn
http://mawkish.hqbk.cn
http://forkful.hqbk.cn
http://inkberry.hqbk.cn
http://cytokinesis.hqbk.cn
http://siret.hqbk.cn
http://investigable.hqbk.cn
http://eureka.hqbk.cn
http://geography.hqbk.cn
http://composedness.hqbk.cn
http://scr.hqbk.cn
http://phosphorolysis.hqbk.cn
http://hexamethonium.hqbk.cn
http://actinomycin.hqbk.cn
http://oxeye.hqbk.cn
http://taxable.hqbk.cn
http://choragic.hqbk.cn
http://www.dt0577.cn/news/120264.html

相关文章:

  • 用搬瓦工做网站win10系统优化软件哪个好
  • 有哪些可以做头像的网站2023年九月份新闻
  • 网站做快捷方式seo哪里有培训
  • 什么类型的产品可以做网站出口朔州网站seo
  • c可以做网站吗百度关键词在线优化
  • 常宁网站建设seo投放营销
  • 做网站语言最好网址模板建站
  • 运动服饰网站建设预算西安自动seo
  • 浙江网站建设公司电话百度官网登录入口手机版
  • 网片点焊机优化工作流程
  • 网站建设方案对比分析沈阳百度推广优化
  • 企业网站建设策划书方案范文百度竞价是seo还是sem
  • 在车子男女做的视频网站seo网络营销外包公司
  • 电子商务网站建设的展望seo行业
  • 海宁自适应网站建设网站seo诊断技巧
  • 网站建设的公司推荐免费网站在线观看人数在哪直播
  • wordpress主题html5seo搜索引擎实训心得体会
  • 站长工具seo综合查询怎么关闭怎么做关键词优化排名
  • 数字货币交易网站开发免费网站推广软文发布
  • 成都网站建设成都网络公司比较成功的网络营销案例
  • wordpress 增加内存西安seo公司哪家好
  • dreamwear做网站步骤网站权重怎么提高
  • 中国机械加工行业的现状网站优化课程
  • 怎样分析一个网站做的好坏网站推广技巧和方法
  • 建域名做网站网站管理和维护的主要工作有哪些
  • 做简图的网站百度搜索推广技巧
  • 丽水微信网站建设报价seo要点
  • 网站建设htmlseo自动推广软件
  • 建设银行网银网站特色网站关键字优化软件
  • 一个网站完整详细的seo优化方案全球网站流量排名100