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

彻底理解TypeScript函数语法

目录

  • 参数类型
    • 基本声明
    • 默认参数
    • 剩余参数
    • 可选只读
    • 匿名函数
    • 回调函数
  • 返回值类型
  • 函数类型
    • 表达式
    • 调用签名
    • 构造签名
  • 函数的重载
  • this
    • 可推导的
    • 编译选项
    • this类型
    • 内置工具

函数是JavaScript非常重要的组成部分,TypeScript中也是如此,TypeScript 提供了强大的类型系统来帮助我们定义函数的参数、返回值以及函数本身的类型

参数类型

基本声明

参数的类型注解:声明函数时,可以在每个参数后添加类型注解,以声明函数接受的参数类型

function add(num1: number, num2: number) {console.log(num1,num2)
}
add(10, 20)
add('10', '20') // Argument of type 'string' is not assignable to parameter of type 'number'

默认参数

如果调用时没有传递该参数,则使用默认值,这个时候greeting的类型其实是 undefinedstring 类型的联合

function greet(name: string, greeting: string = "Hello") {return `${greeting}, ${name}!`;
}console.log(greet("Alice"));              // Hello, Alice!
console.log(greet("Alice", "Good day"));  // Good day, Alice!

剩余参数

使用剩余参数可以让函数接收任意数量的参数,剩余参数会被收集到一个数组中

function sum(...numbers: number[]) {return numbers.reduce((acc, num) => acc + num, 0);
}console.log(sum(1, 2, 3, 4)); // 10

可选只读

  • 可以指定某个参数是可选的,可选类型需要在必传参数的后面,这个参数z依然是有类型的,类型是:number | undefined

    function fo1(x: number, y: number, z?: number) { console.log(x, y, z) // 10 20 undefined
    }
    fo1(10, 20)
    
  • readonly 关键字主要用于对象和数组等复合类型的参数,而对于普通的基本类型(如 number, string 等),它们是按值传递的,没必要使用 readonly

    function foo2(data: { name: string, readonly age?: number }) { console.log(data)
    }
    

匿名函数

匿名函数与函数参数声明会有一些不同:

  • 当一个函数出现在TypeScript可以确定该函数会被如何调用的地方时,该函数的参数会自动指定类型

  • 这个过程称之为上下文类型(contextual typing),因为函数执行的上下文可以帮助确定参数和返回值的类型

const names: string[] = ['abc','cba','eee','ghj']
names.forEach((n => { // 这个n就是string类型,不需要在指定类型console.log(n.toLocaleUpperCase())
}))

回调函数

函数可以作为函数的参数回调,TypeScript对于传入的函数类型的多余的参数会被忽略掉

function executeCallback(callback: (message: string) => void) {callback("This is a callback message");
}executeCallback((msg: string) => {console.log(msg);  // 输出: This is a callback message
});

返回值类型

  • 可以添加返回值的类型注解,这个注解出现在函数列表的后面,返回的类型不对会报错
    在这里插入图片描述

  • 和变量的类型注解一样,通常情况下不需要返回类型注解,因为TypeScript会根据 return 返回值推断函数的返回类型
    在这里插入图片描述

函数类型

表达式

  • 可以编写函数类型的表达式(Function Type Expressions),来表示函数类型 (参数列表) => 返回值类型

  • 在某些语言中,可能参数名称num1num2是可以省略,但是TypeScript是不可以的

  • (x: number, y: number) => number 是函数的类型签名,它表示该函数接受两个 number 类型的参数并返回一个 number
    在这里插入图片描述

type mulType = (num1: number, num2: number) => numberconst mul: mulType = function (num1: number, num2: number){return num1 * num2
}
mul(10,20)

调用签名

函数的调用签名(Call Signatures 是从对象的角度来看待这个函数, 函数也可以有其他属性)

/* 作为函数类型 */
interface ICacl {key: string;(num1: number, num2: number): void;
}const cacl: ICacl = function(num1: number, num2: number): void {console.log(num1, num2);
};// 给函数对象赋予属性 key,不赋值会报错
cacl.key = "someKey";
console.log(cacl.key);  // 输出: "someKey"
cacl(10, 20);  // 输出: 10 20/* 作为参数 */
interface ISign {key: string,(num1: number, num2: number): void
}
function sum(num: number, callBack: ISign) {console.log(callBack.key)callBack(num, 20)
}
const fn: ISign = (num1: number, num2: number) => { }
fn.key = 'params'
sum(10, fn)

构造签名

构造签名(constructor signature)是用于定义类的构造函数类型的语法,定义一个对象或类可以被实例化,描述了使用 new 关键字实例时所需的参数和返回的实例类型

interface IPerson {new (name: string, age: number): Person
}
class Person {name: stringage: numberconstructor(name: string, age: number) {this.name = namethis.age = age}
}
let obj:IPerson = Person
console.log(new obj('obj', 18))

函数的重载

  • TypeScript中,可以去编写不同的重载签名(overload signatures)来表示函数可以以不同的方式进行调用

  • 一般是编写两个或者以上的重载签名,再去编写一个通用的函数以及实现

  • 调用时它会根据传入的参数类型来决定执行函数体时,到底执行哪一个函数的重载签名

/* 在TypeScript中,如果编写一个add函数,希望可以对字符串和数字类型进行相加,应该如何编写呢?
*/
function add1(a1: string | number, a2: string | number, a3: string | number) {return a1 + a2 + a3 // 报错:运算符“+”不能应用于类型“string | number”和“string | number”
}// 编写不同的重载签名
function add(a1: number, a2: number, a3: number): number
function add(a1: string, a2: string): string
function add(a1: any, a2: any, a3?: any): any {return a1 + a2 + a3
}
console.log(add(10, 20, 30)); // 60
console.log(add("aaa", "bbb")); // aaabbbundefined

this

thisJavaScript中一个比较难以理解和把握的知识点,目前的Vue3React开发中你不一定会使用到thisVue3Composition API中和ReactHooks开发中很少见到this,但是还是要简单掌握一些TypeScript中的this

可推导的

默认情况下TypeScript在编译时,this是可以正确去使用的,因为在没有指定this的情况,this默认情况下是any类型的

const bar = {name: 'bar',foo: function (){console.log(this.name)}
}
bar.foo()
function foo(){console.log(this)
}

编译选项

  • 使用命令tsc --init生成tsconfig.json的配置文件时,有的this就会报错

  • 主要由tsconfig.js中的 noImplicitThis 值控制

  • 设置了noImplicitThistrue时, TypeScript会根据上下文推导this,但是在不能正确推导时就会报错,需要明确的指定this

const bar = {name: 'bar',foo: function (){console.log(this.name) // 可以类型推导出this: { name: string; foo: () => void; }}
}
bar.foo()function foo(){console.log(this) // 这时推导不出this的具体类型,修改tsconfig.json中的 noImplicitThis值为 false 就不会报错
}
foo()

this类型

在开启noImplicitThis的情况下,必须指定this的类型:

  • 函数的第一个参数可以根据该函数之后被调用的情况,用于声明this的类型(名词必须叫this

  • 在后续调用函数传入参数时,从第二个参数开始传递的,this参数会在编译后被抹除

const bar = {name: 'bar',foo: function (){console.log(this.name) // 可以类型推导出this: { name: string; foo: () => void; }}
}
bar.foo()function foo(this: {name: string, age: number}, num: number){console.log(this, num) // 打印:name: 'foo', age: 18} 20    这时this的具体类型就是 {name: string, age: number}
}
foo.call({name: 'foo', age: 18}, 20)

内置工具

Typescript 提供了一些this相关的工具来辅助进行常见的类型转换,这些类型全局可用:

  • ThisParameterType<Type>:用于从函数类型中提取 this 参数的类型,如果函数没有明确指定 this 参数,那么 ThisParameterType 会解析为 unknown

    function foo1(this: {name: string, age: number}, num: number){console.log(this, num) // 打印:name: 'foo', age: 18} 20    这时this的具体类型就是 {name: string, age: number}
    }
    foo1.call({name: 'foo', age: 18}, 20)
    type thisType1 = ThisParameterType<typeof foo1> // type thisType = { name: string; age: number;}function foo2(num: number){console.log(num)
    }
    foo2(20)
    type thisType2 = ThisParameterType<typeof foo2> // type thisType2 = unknown
    
  • OmitThisParameter<Type>:从函数类型 Type移除 this 参数。如果 Type 没有 this 参数,或者 Type 不是一个函数类型,那么 OmitThisParameter<Type> 返回 Type 本身

    function foo1(this: {name: string, age: number}, num: number){console.log(this, num) // 打印:name: 'foo', age: 18} 20    这时this的具体类型就是 {name: string, age: number}
    }
    foo1.call({name: 'foo', age: 18}, 20)
    type fnType1 = OmitThisParameter<typeof foo1> // type fnType1 = (num: number) => voidfunction foo2(num: number){console.log(num)
    }
    foo2(20)
    type fnType2 = OmitThisParameter<typeof foo2> // type fnType2 = (num: number) => voidtype fnType3 = OmitThisParameter<string> // type fnType3 = string
    
  • ThisType<T>:这个类型不返回一个转换过的类型,它被用作标记一个上下文的this类型

    • ThisType 只能用于对象字面量,它不能在类或函数中直接使用

    • ThisType<T> 本身并不会在对象里定义 this,它只是告诉 TypeScript 如何推断 this 的类型

    • 需要与 TypeScriptnoImplicitThisnoImplicitAny 编译选项一起使用,确保 this 是明确的

    interface IState {name: stringage: number
    }
    interface IData {state: IStaterunning: () => void
    }
    const info1: IData = {state: {name: 'info1',age: 18,},running() {console.log(this.state.name) // 这时的this类型推导是IData}
    }
    info1.running()const info2: IData & ThisType<IState> = {state: {name: 'info2',age: 18,},running() {console.log(this.name) // 这时的this类型推导是IState}
    }
    info2.running()
    

http://www.mrgr.cn/news/47092.html

相关文章:

  • AIStarter 3.0.2 全新发布:三大模式详解,共创共享共赢!
  • 基于 C# .NET Framework 4.0 开发实现 WCF 服务实例详解(二)——实现Windows服务内嵌WCF服务
  • 【Vue】Vue扫盲(二)指令:v-for 、v-if、v-else-if、v-else、v-show
  • 【力扣刷题实战】(顺序表)移除元素
  • 界面耻辱纪念堂--隐喻使用不当03
  • 国产长芯微LDC5791是一款单通道、20位、无缓冲电压输出DAC完全P2P替代AD5791
  • 如何提高LabVIEW编程效率
  • 2000-2023年上市公司行业异质性数据(东中西、劳动密集型、技术密集型、资本密集型、高科技重污染分组)
  • 2024年最新详解项目管理系统:让你从小白到高手的蜕变
  • vue 深拷贝和浅拷贝的区别
  • java关键字abstract(抽象)详解
  • 数据结构哈夫曼树-哈夫曼树代码构造实现(C语言)
  • VAS1800Q奇力科技线性芯片电荷泵热处理
  • 数据建模?权威指南?来就对了!!!
  • Jquery serialize()、serializeArray()、$.param()
  • 单场数字人直播爆量300万,GMV狂增80%,电商人如何玩转数字人直播?
  • AI时代的SaaS架构变革
  • 不用PS!patchwork快速解决多子图组合问题~~
  • 基于单片机的山林远程环境监测仪设计
  • C盘一红就卡顿到不行?为什么呢?