1

Minor note: Is it possible to include method in TypeScript interface? may seem similar but it's asking the reverse of what this question is.

I have a typescript method that uses an options parameter that works and compiles fine:

  async addKickingRule(options: KickingRuleOptions = {
    time: 60 * MINUTES,
    privileges: ALL_PRIVILEGES
  }) {
    ...
  }

It uses an interface defined elsewhere:


interface KickingRuleOptions {
  time: number,
  privileges: Privileges[],
  channel?: string,
  userID?: string,
  ipAddress?: string
}

Again, this works fine. However I feel it would be easier for my colleagues if the interface was defined in-the body of the method.

Can I use an options object in a typescript method and define the options interface in the body of the method?

0

1 Answer 1

4

you can do something like this:

async addKickingRule(options: {
  time: number,
  privileges: Privileges[],
  channel?: string,
  userID?: string,
  ipAddress?: string
} = {
  time: 60 * MINUTES,
  privileges: ALL_PRIVILEGES
}) {
  ...
}
Sign up to request clarification or add additional context in comments.

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.