shyoushine 2019-08-16
以使用接口的方式来定义一个函数需要符合的形状:
interface SearchFunc { (source: string, subString: string): boolean; } let mySearch: SearchFunc = (source: string, subString: string) => { return source.search(subString) !== -1; }
当然也可以使用含有泛型的接口来定义函数的形状:
interface CreateArrayFunc { <T>(length: number, value: T): Array<T>; } let createArray: CreateArrayFunc = <T extends {}>(length: number, value: T): Array<T> => { let result: T[] = []; for (let i = 0; i < length; i++) { result[i] = value; } return result; } createArray(3, 'x'); // ['x', 'x', 'x']
进一步,我们可以把泛型参数提前到接口名上:
interface CreateArrayFunc<T> { (length: number, value: T): Array<T>; } let createArray: CreateArrayFunc<any>; createArray = function<T>(length: number, value: T): Array<T> { let result: T[] = []; for (let i = 0; i < length; i++) { result[i] = value; } return result; } createArray(3, 'x'); // ['x', 'x', 'x']
注意,此时在使用泛型接口的时候,需要定义泛型的类型。