Typescript object as function argument

Viewed 104
transforms: {
   webpackConfiguration?: ExecutionTransformer<webpack.Configuration>,
} = {} 

While reading some code from an Angular project I found this piece above which gets passed as an function argument. In my understanding the argument transforms is an object with one key value pair, the ? means that webpackConfiguration is optional which confuses me because it is the only key-value-pair entry. What does the = {} mean at the end of the code above?

1 Answers

It is the default value of transforms argument in case you don't provide it. Whatever follows the = is considered to be the default value so, in this case, transforms will be set to a new empty object {} if you don't pass a value to the function. Take the following function fun as example:

function fun(arg1: { bool?: true } = {}, arg2: boolean = false) {

}

If you don't provide arg1, it will be set to {} and if you don't provide arg2, it will be set to false in the function fun.

Related