Home>

I have a simple interest, but I want to define something like a function with properties in TypeScript.
Is this possible in the first place?interfacecould be defined as follows:

interface Hoge {
  a: string;
  (arg: string): void;
}

I'm having trouble understanding how to initialize.

const f: Hoge = (arg: string) =>{console.log (arg);};
// Property 'a' is missing in type '(arg: string) =>void' but required in type 'Hoge'.

I would be happy if you could tell me.

  • Answer # 1

    Teached by kind experts and solved it!

    interface Hoge {
        a: string;
        (arg: string): void;
    }
    const f: Hoge = Object.assign (
        (arg: string) =>{console.log (arg);},
        {a: "foo"}
    )
    // or
    const f = ((arg: string) =>console.log (arg)) as Hoge
    f.a = 'foo'

Related articles