还是Angular的新手...
研究了文档并搜索了网络之后,我似乎无法弄清楚如何导出返回函数的函数。尝试类似的东西:
export function generateValidCharacterGenerator(validCharacters: string): function(control: FormControl): any {
const result = function (control: FormControl): any {
};
return result;
}
(我通过阅读https://www.typescriptlang.org/docs/handbook/functions.html进行了尝试)
但是我尝试的所有内容都会出现语法错误。你可以做到的吧?
答案 0 :(得分:1)
export var generateValidCharacterGenerator : (validCharacters: string) => (control: FormControl)=> any = (validCharacters: string) => {
const result = (control: FormControl): any => {
};
return result;
}
我建议您使用箭头功能代替function
,因为它保留了this
。
答案 1 :(得分:1)
在TypeScript中,请勿使用单词function
将函数声明为类型。
例如;
// this is a function
function foo(): number {
return 4;
}
// this is a type
type foo = () => number;
箭头功能既是功能又是类型。
例如;
// this is a function
const foo = () => 4;
箭头功能是JavaScript的功能。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
箭头类型是TypeScript的功能。
https://www.typescriptlang.org/docs/handbook/functions.html
当您将箭头写为一种类型时,它们没有功能主体。参数产生一个返回类型,例如:(a: boolean) => number
接受一个布尔值并返回一个数字。当箭头是函数时,它具有主体和返回类型,例如:(a: boolean): number => 4;
这取决于您在哪里使用它们。