#How do you implement `interface Foo { (...args: any[]): Bar; }`?

19 messages · Page 1 of 1 (latest)

spiral oceanBOT
#
bawdyinkslinger#0

Preview:```ts
type Bar = {}

interface Foo {
(...args: any[]): Bar
}

const x: Foo = {
// how do you implement Foo?
}```

dire sandal
#

!ts

spiral oceanBOT
#
type Bar = {};

interface Foo {
  (...args: any[]): Bar;
}

const x: Foo = {
//    ^
// Type '{}' is not assignable to type 'Foo'.
//   Type '{}' provides no match for the signature '(...args: any[]): Bar'.
  // how do you implement Foo?
};
dire sandal
#

Is there a way to implement this with an object literal?

#

I don't know how to implement it regardless

river forge
#

It's a function

dire sandal
#

I tried assuming it was a function, and still couldn't get it to compile

#

oh nm

#

const x: Foo = () => { return {}}

#

I guess it's not syntactically correct unless I wrap the body in curly braces

river forge
#

you can also use parenthesis

#

const x: Foo = () => ({})

dire sandal
#

okay thanks

#

!resolve

#

Actually, why would you define a function type through an interface?

#

is it only useful if you want to do fine other properties on the function?

river forge
#

you can define overloads as well