Coming from a C# background, I want to create a datatype that defines a function signature. In C#, this is a delegate
declared like this:
delegate v
You can create something like a delegate by using a type alias:
type MyDelegate = (input: string) => void;
which defines a type name for a function pointer, just as delegates do in C#. The following example uses it in combination with generic type parameters:
type Predicate = (item: T) => boolean;
export class List extends Array {
constructor(...items: T[]){
super();
for(let i of items || []){
this.push(i);
}
}
public hasAny(predicate?: Predicate): boolean {
predicate = predicate || (i => true)
for(let item of this) {
if(predicate(item)) return true;
}
return false;
}
}