2

I am in the process of creating TypeScript definitions for RiotJS. This lib uses nested functions like so:

riot.route( args ); // function
riot.route.parser( args ); // function in function

Here is an extract of what I have so far:

declare module riot
{
    export function route( callback : Function ) : void
    export function route( to : string ) : void
}

I am not sure how to structure the definition for the "riot.route.parser()" nested function and wondering if anyone has any insight to share on this ?

Thank you

1 Answer 1

5

Here is an implementation that uses inline typing :

declare var riot: {
    route: {
        (callback: Function): void
        parser: {
            (args): any;
        }
    };

}

riot.route(() => null); // function
riot.route.parser(1); // function in function

I recommend you break it up into interfaces, naming them according to a.) either purpose or b.) how actual documentation calls them.

An example to get you started:

interface Riot {
    route: {
        (callback: Function): void
        parser: {
            (args): any;
        }
    };

}
declare var riot:Riot;

riot.route(() => null); // function
riot.route.parser(1); // function in function

Update

for function overloads where the return type / number of arguments do not change I recommend Union Types:

interface Riot {
    route: {
        (toOrCallback: string|Function): void
        (callback: Function): void
        parser: {
            (args): any;
        }
    };

}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Out of interest how would you overload the 'route' function with definition. i.e. : 'export function route( callback : Function ) : void export function route( to : string ) : void' Or would you just just any for the callback ?
Clever - I would give you another thumbs up for that if I could. Thank you again .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.