在TypeScript中,我可以将函数的形参声明为function类型。是否有一种“类型安全”的方式来做到这一点,我错过了?例如,考虑这个:
class Foo {
save(callback: Function) : void {
//Do the save
var result : number = 42; //We get a number from the save operation
//Can I at compile-time ensure the callback accepts a single parameter of type number somehow?
callback(result);
}
}
var foo = new Foo();
var callback = (result: string) : void => {
alert(result);
}
foo.save(callback);
保存回调是不类型安全的,我给它一个回调函数,其中函数的参数是一个字符串,但我传递给它一个数字,并编译没有错误。我可以使结果参数在保存类型安全的函数?
TL;DR版本:在TypeScript中有等价的。net委托吗?
当然。函数的类型由其实参类型和返回类型组成。这里我们指定回调参数的类型必须是“接受数字并返回any类型的函数”:
class Foo {
save(callback: (n: number) => any) : void {
callback(42);
}
}
var foo = new Foo();
var strCallback = (result: string) : void => {
alert(result);
}
var numCallback = (result: number) : void => {
alert(result.toString());
}
foo.save(strCallback); // not OK
foo.save(numCallback); // OK
如果你愿意,你可以定义一个类型别名来封装它:
type NumberCallback = (n: number) => any;
class Foo {
// Equivalent
save(callback: NumberCallback) : void {
callback(42);
}
}
我知道这篇文章很旧了,但有一个更紧凑的方法,与所要求的略有不同,但可能是一个非常有用的替代方案。你可以在调用方法时内联声明这个函数(在本例中是Foo的save())。它看起来是这样的:
class Foo {
save(callback: (n: number) => any) : void {
callback(42)
}
multipleCallbacks(firstCallback: (s: string) => void, secondCallback: (b: boolean) => boolean): void {
firstCallback("hello world")
let result: boolean = secondCallback(true)
console.log("Resulting boolean: " + result)
}
}
var foo = new Foo()
// Single callback example.
// Just like with @RyanCavanaugh's approach, ensure the parameter(s) and return
// types match the declared types above in the `save()` method definition.
foo.save((newNumber: number) => {
console.log("Some number: " + newNumber)
// This is optional, since "any" is the declared return type.
return newNumber
})
// Multiple callbacks example.
// Each call is on a separate line for clarity.
// Note that `firstCallback()` has a void return type, while the second is boolean.
foo.multipleCallbacks(
(s: string) => {
console.log("Some string: " + s)
},
(b: boolean) => {
console.log("Some boolean: " + b)
let result = b && false
return result
}
)
对于可能成功或失败的网络调用之类的事情,multipleCallback()方法非常有用。再次假设一个网络调用示例,当调用multipleCallbacks()时,成功和失败的行为都可以在一个地方定义,这有助于将来的代码读者更加清晰。
一般来说,根据我的经验,这种方法更简洁,更少混乱,整体上更清晰。
祝大家好运!
如果你先定义函数类型,那么它看起来就像
type Callback = (n: number) => void;
class Foo {
save(callback: Callback) : void {
callback(42);
}
}
var foo = new Foo();
var stringCallback = (result: string) : void => {
console.log(result);
}
var numberCallback = (result: number) : void => {
console.log(result);
}
foo.save(stringCallback); //--will be showing error
foo.save(numberCallback);
如果没有使用普通属性语法的函数类型,它将是:
class Foo {
save(callback: (n: number) => void) : void {
callback(42);
}
}
var foo = new Foo();
var stringCallback = (result: string) : void => {
console.log(result);
}
var numberCallback = (result: number) : void => {
console.log(result);
}
foo.save(stringCallback); //--will be showing error
foo.save(numberCallback);
如果你想使用一个接口函数,如c#泛型委托,它将是:
interface CallBackFunc<T, U>
{
(input:T): U;
};
class Foo {
save(callback: CallBackFunc<number,void>) : void {
callback(42);
}
}
var foo = new Foo();
var stringCallback = (result: string) : void => {
console.log(result);
}
var numberCallback = (result: number) : void => {
console.log(result);
}
let strCBObj:CallBackFunc<string,void> = stringCallback;
let numberCBObj:CallBackFunc<number,void> = numberCallback;
foo.save(strCBObj); //--will be showing error
foo.save(numberCBObj);
在TS中,我们可以用以下方式输入函数:
函数类型/签名
它用于函数/方法的实际实现,语法如下:
(arg1: Arg1type, arg2: Arg2type) : ReturnType
例子:
function add(x: number, y: number): number {
return x + y;
}
class Date {
setTime(time: number): number {
// ...
}
}
函数类型文字
函数类型字面量是声明函数类型的另一种方式。它们通常应用于高阶函数的函数签名中。高阶函数是接受函数作为参数或返回函数的函数。它的语法如下:
(arg1: Arg1type, arg2: Arg2type) => ReturnType
例子:
type FunctionType1 = (x: string, y: number) => number;
class Foo {
save(callback: (str: string) => void) {
// ...
}
doStuff(callback: FunctionType1) {
// ...
}
}
除了其他人所说的,一个常见的问题是声明重载的同一个函数的类型。典型的例子是EventEmitter on()方法,它将接受多种侦听器。当使用redux动作时,也会发生类似的情况-在那里你使用动作类型作为文字来标记重载,在eventemitter的情况下,你使用事件名称
文字类型:
interface MyEmitter extends EventEmitter {
on(name:'click', l: ClickListener):void
on(name:'move', l: MoveListener):void
on(name:'die', l: DieListener):void
//and a generic one
on(name:string, l:(...a:any[])=>any):void
}
type ClickListener = (e:ClickEvent)=>void
type MoveListener = (e:MoveEvent)=>void
... etc
// will type check the correct listener when writing something like:
myEmitter.on('click', e=>...<--- autocompletion
因为不能轻易地将函数定义和另一种数据类型结合起来,所以我发现使用这些类型对强类型很有用。根据德鲁的回答。
type Func<TArgs extends any[], TResult> = (...args: TArgs) => TResult;
//Syntax sugar
type Action<TArgs extends any[]> = Func<TArgs, undefined>;
现在您可以强类型每个参数和返回类型!下面是一个比上面的参数更多的例子。
save(callback: Func<[string, Object, boolean], number>): number
{
let str = "";
let obj = {};
let bool = true;
let result: number = callback(str, obj, bool);
return result;
}
现在,您可以编写联合类型,如对象或返回对象的函数,而无需创建可能需要导出或使用的全新类型。
//THIS DOESN'T WORK
let myVar1: boolean | (parameters: object) => boolean;
//This works, but requires a type be defined each time
type myBoolFunc = (parameters: object) => boolean;
let myVar1: boolean | myBoolFunc;
//This works, with a generic type that can be used anywhere
let myVar2: boolean | Func<[object], boolean>;
function callbackTesting(callbacks: {onYes: (data: any) => void,onNo: (data: any) => void,onError: (err: any) => void,}, type: String){
switch(type){
case "one":
callbacks.onYes("Print yes");
break;
case "two":
callbacks.onNo("Print no");
break;
default:
callbacks.onError("Print error");
break;
}
}
const onYes1 = (data: any) : void => {
console.log(data);
}
const onNo1 = (data: any) : void => {
console.log(data);
}
const onError1 = (data: any) : void => {
console.log(data);
}
callbackTesting({onYes: function (data: any) {onYes1(data);},onNo: function (data: any) {onNo1(data);},onError: function (data: any) {onError1(data);}}, "one");
callbackTesting({onYes: function (data: any) {onYes1(data);},onNo: function (data: any) {onNo1(data);},onError: function (data: any) {onError1(data);}}, "two");
callbackTesting({onYes: function (data: any) {onYes1(data);},onNo: function (data: any) {onNo1(data);},onError: function (data: any) {onError1(data);}}, "cfhvgjbhkjlkm");