1

Can you pass default values to a class constructor? Here, last returns undefined.

class greatFunction {
    options: {
        names:  {first: 'Joe', last: 'Bloggs'}
    }
    constructor(options) {
        console.log(options.names.first + ' ' + options.names.last);        
    }
}

Called:

new greatFunction({
    names: {
        first: 'Paul'
    }
});
3
  • I am not familiar with typescript but, within your constructor you could set the default by using options.names.last = options.name.last || "default" if the first side of the check (left from ||) is true, that value is taken, if it is false, the right side is taken. Since undefined is false, it defaults to "default" Commented Aug 22, 2016 at 10:48
  • @Dellirium Yup I know that but looking for tidier way of doing it. Thanks! Commented Aug 22, 2016 at 10:49
  • You don't want your param name to be the same as a local variable Commented Aug 22, 2016 at 10:50

3 Answers 3

3

instead of doing that why dont you create an interface like this

interface names { 
      firstname:string;
      lastname ?: string;
  }

Since you can't create make a property of a parameter optional.

Or you do the hard way which is like this

interface IOptions{
names:INames;
     }

 interface  INames {
    first:string;
    last?:string;
  }

   class greatFunction{

        constructor (options:IOptions){
          options.names.last = options.names.last?options.names.last:'Default';
       console.log(options.names.first + ' ' + options.names.last)
    }
 }

   new greatFunction({names:{first:'Theophilus'}});

It should return the default in console

Sign up to request clarification or add additional context in comments.

Comments

0

Try setting default values:

class GreatFunction {
  options: {
    names:  {first: 'Joe', last: 'Bloggs'}
  }
  constructor({names: {first=this.options.names.first: firstName, last=this.options.names.last: lastName}}) {
    console.log(`${firstName} ${lastName}`)        
  }
}

Alternatively, use a library, for example lodash.defaults() seems to fit your use case.

2 Comments

"The language only supports setting a default for function parameters themselves" this refers to typescript, or javascript (in case of javascript, how? o.0)
Both, really... constructor({first=this.options.first, last=this.options.last}) { console.log(first + ' ' + last)}
0

You do need to pass something to the function, so one option would be to try something like this:

class greatFunction {
    defaultOptions = {
        names: { first: 'Joe', last: 'Bloggs' }
    }
    constructor(options) {
        if (options == null)
            options = this.defaultOptions;
        console.log(options.names.first + ' ' + options.names.last);
    }
} 

You can then create a new greatFunction using:

let x = new greatFunction(null)

Or:

let y = new greatFunction({ names: { first: 'Test', last: 'Blah' } });

Alternatively, don't do it using the constructor, and use a property after initialising. For example:

class greatFunction {
    public options = {
        names: { first: 'Joe', last: 'Bloggs' }
    }
    constructor() { }
    doSomething() {
        console.log(this.options.names.first + ' ' + this.options.names.last);
    }
} 

and then use with:

let x = new greatFunction();
x.options = { names: { first: 'Test', last: 'Blah' } };
x.doSomething();

1 Comment

This is good, but what if all options are optional, and if the option is not passed through it will use the default for that option. I don't want it to be either pass all options or pass none.

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.