1

I have an interface that has a property matchMaker, it looks like this:

export interface ServerOptions {
  matchMaker: MatchMakerType<MatchMaker>
}

I then have a class that sets up default values for to utilize that interface which looks like this:

export class Server {
  public readonly settings: ServerOptions = {
    matchMaker: undefined
  }

  public constructor(options: ServerOptions) {
    this.settings = Object.assign(this.settings, options)
  }
}

When a new instance of the class Server is created, I want to require a typeof MatchMaker to be set for matchMaker when passing the options to the constructor. I don't want to allow undefined | null or anything else to be passed to the constructor, however I need to have some value as the default, and when I use undefined which will get overridden, I get an error:

Type 'undefined' is not assignable to type 'MatchMakerType'.

What can I do to set up a default that will be overridden?

0

1 Answer 1

3

You could use Partial, to allow not defined properties in Server.settings:

 public readonly settings: Partial<ServerOptions> = {  }
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.