Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any reason to put implements interface to class?

Tags:

typescript

I'm new to type script, just browsing their tutorial and trying some coding in their playground but found something strange.

For example this code:

class foobar implements Ifoobar
{
    full: string;
    constructor (public foo, public bar)
    {
        this.full = foo + bar;
    }
}

interface Ifoobar
{
    foo: string;
    bar: string;
}

function test(ifoobar: Ifoobar)
{
    return ifoobar.foo + ifoobar.bar;
}

var obj = new foobar("hello", "world");


document.body.innerHTML = test(obj);

works if you put

class foobar implements Ifoobar

or just

class foobar 

so what's the point of using interface if contract itself was not enforced?

UPDATE My main concern is actually on this line:

document.body.innerHTML = test(obj);

this should throw error right since foobar doesn't use implements Ifoobar, and test(ifoobar: Ifoobar) as specified in method argument should accept only Ifoobar. To me it feels like typescript just plainly think that foobar is implements Ifoobar even though it isn't.

like image 343
kirie Avatar asked Nov 19 '25 16:11

kirie


2 Answers

TypeScript uses duck typing, which means if the members match, types are considered compatible. That's why the code is still valid after removing implements Ifoobar - a foobar can be treated as a Ifoobar since it has all the members declared by Ifoobar.

And TypeScript does enforce the contract. If you remove/rename either member foo or bar, the compiler will generate errors.

like image 138
Gildor Avatar answered Nov 21 '25 06:11

Gildor


what's the point of using interface if contract itself was not enforced

Its enforced at compile time e.g. the following fails to compile (because I removed public from bar):

class foobar implements Ifoobar // ERROR property `bar` is missing in foobar 
{
    full: string;
    constructor (public foo, bar)
    {
        this.full = foo + bar;
    }
}

interface Ifoobar
{
    foo: string;
    bar: string;
}

It is really for the class author (not consumer) to ensure that they follow the contract (interface).

More

Please note that types are structuring in typescript and compile failure does not mean that js will not be generated. See why typescript

like image 24
basarat Avatar answered Nov 21 '25 04:11

basarat