Reputation: 13
I'm trying to write a recursive typescript family of functions that takes an array with elements of its own type as a parameter.
function example(parameter:number, path: {(parameter:number, path:{/*what do I put here?!*/}[]):boolean;}[]) : boolean
{
return false;
}
This means I could call the function with:
let result = example(123, [example, example, anotherexample]);
The path / "What do I put here" part is where I'm stuck. I'd like to put the whole function type into a typedef somehow, also to improve readability.
Upvotes: 0
Views: 7769
Reputation: 15096
You can declare the type of example
as an interface, so you can refer to it in the type for path
:
interface Example {
(parameter: number, path: Example[]): boolean
}
function example(parameter: number, path: Example[]): boolean {
return false;
}
UPD: To explicitly declare the type of example
, you can write it like this:
const example : Example = function (parameter: number, path: Example[]): boolean {
return false;
}
This will warn for type errors, but note that it's a constant now, so you won't be able to refer to it before its declaration. For more info on interfaces, check out https://www.typescriptlang.org/docs/handbook/interfaces.html
Upvotes: 2