在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委托吗?


当前回答

我知道这篇文章很旧了,但有一个更紧凑的方法,与所要求的略有不同,但可能是一个非常有用的替代方案。你可以在调用方法时内联声明这个函数(在本例中是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()时,成功和失败的行为都可以在一个地方定义,这有助于将来的代码读者更加清晰。

一般来说,根据我的经验,这种方法更简洁,更少混乱,整体上更清晰。

祝大家好运!

其他回答

当然。函数的类型由其实参类型和返回类型组成。这里我们指定回调参数的类型必须是“接受数字并返回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);
    }
}

下面是一些常见的。net委托的TypeScript等价版本:

interface Action<T>
{
    (item: T): void;
}

interface Func<T,TResult>
{
    (item: T): TResult;
}
type FunctionName = (n: inputType) => any;

class ClassName {
    save(callback: FunctionName) : void {
        callback(data);
    }
}

这当然符合函数式编程范式。

因为不能轻易地将函数定义和另一种数据类型结合起来,所以我发现使用这些类型对强类型很有用。根据德鲁的回答。

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");