Generic Interfaces

suggest change

Declaring a generic interface

interface IResult<T> {
    wasSuccessfull: boolean;
    error: T;
}

var result: IResult<string> = ....
var error: string = result.error;

Generic interface with multiple type parameters

interface IRunnable<T, U> {
    run(input: T): U;
}

var runnable: IRunnable<string, number> = ...
var input: string;
var result: number = runnable.run(input);

Implementing a generic interface

interface IResult<T>{
    wasSuccessfull: boolean;
    error: T;

    clone(): IResult<T>;
}

Implement it with generic class:

class Result<T> implements IResult<T> {
    constructor(public result: boolean, public error: T) {
    }

    public clone(): IResult<T> {
        return new Result<T>(this.result, this.error);
    }
}

Implement it with non generic class:

class StringResult implements IResult<string> {
    constructor(public result: boolean, public error: string) {
    }

    public clone(): IResult<string> {
        return new StringResult(this.result, this.error);
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents