user10103655
user10103655

Reputation: 131

How to call a method with an interface param with a function type and additional types

It's possible to create an interface with the following shape:

export interface GenericType<T> {
        (data: T): void;
        hasLimit?: boolean;
    }

But how do I supply an object that implements the interface for it?

I could change the interface to:

export interface GenericType<T> {
        callback: (data: T) => void;
        hasLimit?: boolean;
    }

And supply an object that fits the shape with:

methodname({callback, true})

But how can I supply an object with the boolean parameter without changing the interface?

Upvotes: 0

Views: 146

Answers (1)

zerkms
zerkms

Reputation: 255105

You cannot create it with a single statement, but instead should declare a variable and use the properties:

const f = (data: string): void => { 
    console.log(data);
};
f.hasLimit = true;

Playground: https://www.typescriptlang.org/play/?ssl=9&ssc=19&pln=6&pc=1#code/JYOwLgpgTgZghgYwgAgOIRNYCAqBPABwgB4cA+ZAbwFgAoZB5ACgBM4w4AuZHASm4BuAe2AsA3HUbIAFnADOAGWABbYGAD83AEZChAGwhwQE2gF86dBEJBywyGMgC8zNh262ooAOb9kw0U4UlMiSjFY2+hAAdHpCXqzscLwmpiYwUbKKKmpOyGBQAK4QJpbWtshcaBhYuIQkHt4UzjBiQA

Upvotes: 1

Related Questions