0

How can I use following interface?

interface Something {
 (n: number): void,
 description: string
}

I try writing following.

var sth: Something = {
    description: "Description"
}

sth = (n: number) => {
    console.log("Something");
}

or

var sth: Something = {
    (n: number) => {
        console.log("Something");
    },
    description: "Description"
}

But they both give errors.

0

2 Answers 2

2

You cannot directly create an object which is both a function and has other properties. You have to do it in two steps, first create a function, then assign it another prop, then assign the result to a variable of your interface:

interface Something {
  (n: number): void;
  description: string;
}

function fn(n: number) {}

fn.description = 'foo';

const h: Something = fn;
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it in one of few ways. Each fits slightly odd with functions with properties but all are viable, depending on the use case:

Create the function then add the property

There is no syntax for creating a function with a property. You have to do both separately:

var sth: Something = ((n: number) => {
    console.log(n + 2);
}) as Something;
sth.description = "Description";

Playground Link

The as Something assertion is needed here to tell the compiler to treat the function as Something, otherwise you cannot add the description.

Object.assign()

This allows declaring everything at once:

var sth: Something = Object.assign(
  (n: number) => {
    console.log(n + 2);
  },
  { description: "Description"}
);

Playground Link

Object.assign is typed to return any so the type declaration sth: Something makes sure it is treated as the correct object afterwards.

Object.defineProperty()

Another way to create an object and add a property. It allows more control over the property, like making it non-writable, if needed.

var sth: Something = Object.defineProperty(
  (n: number) => {
    console.log(n + 2);
  },
  "description",
  { value: "Description"}
) as Something;

Playground Link

The as Something assertion is needed because Object.defineProperty() is typed to return the same as the first argument. So it returns (n: number) => void.

Object.defineProperties()

Very similar to Object.defineProperty() but allows multiple properties to be configured at once.

var sth: Something = Object.defineProperties(
  (n: number) => {
    console.log(n + 2);
  },
  { 
    description: { value: "Description" }
  }
) as Something;

Playground Link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.