TypeScript Type of generic function

后端 未结 2 1072
失恋的感觉
失恋的感觉 2021-02-20 01:52

I can\'t get my head arround the following paragraph in the TypeScript documentation:

\"The type of generic functions is just like those of non-generic functions, with t

2条回答
  •  后悔当初
    2021-02-20 02:54

    The last line declares a variable named myIdentity. The variable is of a function type, a generic function (the makes the it the signature of a generic function, more type arguments could be in the list) which takes an argument of type T and returns a value of typeT. And then initializes the variable with the identity function which conforms to the declared signature of myIdentity.

    You may want to do this in order to assign different functions to myIdentity based on runtime conditions. Or declare a parameter of this type and pass it to a function that can invoke it later.

提交回复
热议问题