TypeScript【可选属性、只读属性、额外的属性检查、函数类型、类类型、继承接口】(四)-全面详解(学习总结---从入门到深化)

简介: TypeScript【可选属性、只读属性、额外的属性检查、函数类型、类类型、继承接口】(四)-全面详解(学习总结---从入门到深化)

接口_可选属性



接口里的属性不全都是必需的。 有些是只在某些条件下存在,或者根本不存在。 可选属性在应用 “option bags” 模式时很常用,即给函数传入的参数对象中只有部分属性赋值了


带有可选属性的接口与普通的接口定义差不多,只是在可选属性名字定义的后面加一个 ? 符号

interface SquareConfig {
  color?: string;
  width?: number;
}
function createSquare(config: SquareConfig)
{
  let newSquare = {
    color: "white",
    area: 100
 }
  if (config.color) {
    newSquare.color = config.color;
 }
  if (config.width) {
    newSquare.area = config.width * config.width;
 }
  return newSquare;
}
let mySquare = createSquare({ color: "black"
});
console.log(mySquare)


可选属性的好处之一是可以对可能存在的属性进行预定义,好处之二是可以捕获引用了不存在的属性时的错误。 比如,我们故意将 createSquare 里的 color 属性名拼错,就会得到一个错误提示

interface SquareConfig {
  color?: string;
  width?: number;
}
function createSquare(config:SquareConfig){
  let newSquare = { color: "white", area: 100 };
  if (config.clor) {
    // Error: Property 'clor' does not exist on type 'SquareConfig'
    newSquare.color = config.clor;
 }
  if (config.width) {
    newSquare.area = config.width * config.width;
 }
  return newSquare;
}
let mySquare = createSquare({ color: "black"});


接口_只读属性



一些对象属性只能在对象刚刚创建的时候赋值。 你可以在属性名前用 readonly 来指定只读属性


对象操作

interface Point {
  readonly x: number;
  readonly y: number;
}
let p1: Point = { x: 10, y: 20 };
p1.x = 5; // error!


函数操作

interface Point {
  readonly x: number;
  readonly y: number;
}
function MyPoint(ps:Point){
  const pt = {
    x:100,
    y:200
 }
  // Cannot assign to 'x' because it is a read-only property.
  ps.x = pt.x;
  ps.y = pt.y
  console.log(ps)
}
MyPoint({ x:100,y:200 })


readonly vs const


最简单判断该用 readonly 还是 const 的方法是看要把它做为变量使用还是做为一个属性。 做为变量使用的话用 const ,若做为属性则使用 readonly


接口_额外的属性检查



我们知道。接口具有“option bags” 模式,那我们来看下面的例子

interface SquareConfig {
  color?: string;
  width?: number;
}
function createSquare(config: SquareConfig) {
    console.log(config)
}
let mySquare = createSquare({width: 100 });


现在多了一个需求,在传入的参数中,可能会传入未知参数 colour 因为有“option bags” 模式,会不会允许我们传入未知参数呢?

interface SquareConfig {
  color?: string;
  width?: number;
}
function createSquare(config: SquareConfig) { }
let mySquare = createSquare({ colour: "red",width: 100 });


然而,TypeScript 会认为这段代码可能存在 bug


对象字面量会被特殊对待而且会经过“额外属性检查”,你会得到一个错误

error: Object literal may only specify known properties, but 'colour' does not exist in type 'SquareConfig'. Did you mean to write 'color'?


我们可以通过添加额外属性的方式解决这个问题

interface SquareConfig {
  color?: string;
  width?: number;
 [propName: string]: any;
}
function createSquare(config: SquareConfig)
{
  console.log(config)
}
let mySquare = createSquare({ colour: "red", width: 100 });


还有最后一种跳过这些检查的方式,这可能会让你感到惊讶,它就是将这个对象赋值给一个另一个变量: 因为 squareOptions 不会经过额外属性检查,所以编译器不会报错

interface SquareConfig {
  color?: string;
  width?: number;
}
function createSquare(config: SquareConfig)
{
  console.log(config)
}
const squareObj = { colour: "red", width: 100 }
let mySquare = createSquare(squareObj);


要留意,在像上面一样的代码里,你可能不应该去绕开这些检查,这不利于代码可读性与纠错性


接口_ 函数类型



接口能够描述 JavaScript 中对象拥有的各种各样的外形。 除了描述带有属性的普通对象外,接口也可以描述函数类型

interface SearchFunc {
 (source: string, subString: string)
}


这样定义后,我们可以像使用其它接口一样使用这个函数类型的接口

let mySearch: SearchFunc;
mySearch = function (source: string, subString: string) {
  console.log(source,subString)
};
mySearch("Hello","World")


对于函数类型的类型检查来说,函数的参数名不需要与接口里定义的名字相匹配

interface SearchFunc {
 (source: string, subString: string)
}
let mySearch: SearchFunc;
mySearch = function (sou: string, sub: string) {
  console.log(sou,sub)
}
mySearch("Hello","World")


接口_类类型



与传统面向对象里接口的基本作用一样,TypeScript 也能够用它来明确的强制一个类去符合某种契约

interface UserInterface {
  job:string
}
class Teacher implements UserInterface {
  public job:string
  public name:string
  public age:string
  constructor(name,age){
    this.name = name;
    this.age = age
 }
}


你也可以在接口中描述一个方法,在类里实现它

interface UserInterface {
  job:string
  sayHello(j:string):void
}
class Teacher implements UserInterface {
  public job:string
  public name:string
  public age:string
  constructor(name,age){
    this.name = name;
    this.age = age
 }
  sayHello(j: string) {
    this.job = j
    console.log(j,this.name,this.age)
 }
}
const t = new Teacher("张三","20");
t.sayHello("itxiaotong")


接口_继承接口



和类一样,接口也可以相互继承。 这让我们能够从一个接口里复制成员到另一个接口里,可以更灵活地将接口分割到可重用的模块里

interface Info{
  name:string
  age:number
}
interface Job extends Info{
  job:string
}
function user(userInfo:Job){
  console.log(userInfo)
}
user({ name:"iwen",age:20,job:"itxiaotong" })


一个接口可以继承多个接口,创建出多个接口的合成接口

interface Info{ 
  name:string
  age:number
}
interface Num{
  num:number
}
interface Job extends Info,Num{
  job:string
}
function user(userInfo:Job){
  console.log(userInfo)
}
user({
    name:"iwen",age:20,job:"itbaizhan",num:1001
})
目录
相关文章
|
9月前
|
JavaScript 前端开发 安全
TypeScript 终极入门指南:从零到精通 🚀
TypeScript是JavaScript的超集,添加静态类型系统,提升代码健壮性与可维护性。本教程涵盖基础类型、高级特性、面向对象编程及最佳实践,配代码示例与图解,助你快速掌握TS核心概念,轻松进阶前端开发!🎉
889 2
TypeScript 终极入门指南:从零到精通 🚀
|
人工智能 JavaScript 程序员
一文彻底搞明白HarmonyOS基础TypeScript中的泛型函数
程序员Feri是一位拥有12年+经验的技术专家,擅长嵌入式、鸿蒙、人工智能和Java等领域。本文深入探讨TypeScript中的泛型函数,涵盖基础语法、类型约束、高级技巧及应用场景。通过泛型函数,实现代码逻辑与具体类型的解耦,提升类型安全性和复用性。内容包括集合操作、API抽象、工具类开发等实际应用,以及条件类型、默认类型参数和工具类型的高级技巧。最后提醒开发者注意过度泛型化和性能权衡问题,总结泛型函数在TypeScript类型系统中的核心地位及其未来发展方向。
362 1
一文彻底搞明白HarmonyOS基础TypeScript中的泛型函数
|
移动开发 JavaScript 前端开发
TypeScript:数组类型&函数使用&内置对象
本文介绍了 TypeScript 中的数组类型、对象数组、二维数组、函数、函数重载、内置对象等概念,并通过代码示例详细展示了它们的使用方法。还提供了一个使用 HTML5 Canvas 实现的下雨效果的小案例。
330 1
|
JavaScript 前端开发 编译器
【小白入门】 浏览器如何识别Typescript?
【10月更文挑战第1天】浏览器如何识别Typescript?
|
JavaScript
typeScript基础(7)_函数的类型
本文介绍了TypeScript中函数的类型,包括函数声明与函数表达式的类型注解,如何定义函数的参数类型、返回类型,以及可选参数和参数默认值。还探讨了函数的剩余参数、如何使用接口定义函数的形状,以及函数重载的概念和实践。
335 1
|
JavaScript 索引
TypeScript(TS)安装指南与基础教程学习全攻略(二)
TypeScript(TS)安装指南与基础教程学习全攻略(二)
278 0
|
JavaScript 前端开发 安全
TypeScript(TS)安装指南与基础教程学习全攻略(一)
TypeScript(TS)安装指南与基础教程学习全攻略(一)
862 0
|
JavaScript 前端开发 编译器
TypeScript,从0到入门带你进入类型的世界
该文章提供了TypeScript的入门指南,从安装配置到基础语法,再到高级特性如泛型、接口等的使用,帮助初学者快速掌握TypeScript的基本用法。
|
11月前
|
JavaScript 前端开发
揭秘 TypeScript 条件类型:超越简单类型检查
揭秘 TypeScript 条件类型:超越简单类型检查
|
11月前
|
JavaScript 安全 索引
TypeScript 高级类型工具:Partial, Required, Record 的妙用与陷阱
TypeScript 高级类型工具:Partial, Required, Record 的妙用与陷阱