1、函数类型表达式
// 使用类型别名
type GreetFunction = (a:string)=>void
function greeter(fn:GreetFunction){
fn('hello world')
}
function printToConsole(s:string){
console.log(s);
}
greeter(printToConsole)
2、调用签名
type DescribableFunction = {
description:string,
(someAag:number):boolean
}
function doSomething(fn:DescribableFunction){
console.log(fn.description+' returned '+fn(6));
}
function fn1(n:number){
console.log(n);
return true
}
fn1.description = 'hello'
doSomething(fn1)
3、构造签名
class Ctor{
s:string
constructor(s:string){
this.s = s
}
}
type SomeConstructor = {
new (s:string):Ctor
}
function fn(ctor:SomeConstructor){
return new ctor('hello')
}
const f = fn(Ctor)
console.log(f.s);