1

I'm fairly new to TS and wanted some advice on how to pass an object literal to a method in the most efficient manner.

E.g. (called in html)

<script>
    app.setOptions({
        width: 820,
        height: 450,
        bgColor: '#fff'
    });
</script>

In main.ts

module app {

    app.setOptions = function(options: { width: number, height: number, bgColor: string}) {
        options.width   = options.width || 600,
        options.height  = options.height || 300,
        options.bgColor = options.bgColor || '#fff';

        initializeApp(options);
    }
}

Note: This works fine, although I'm warned about 'the property setOptions does not exist on type 'typeof app'. I'm going to add loads more options too.

Paul Redmond
  • 3,276
  • 4
  • 32
  • 52
  • `options.width = options.width || 600` What if the width is supposed to be 0? – T.J. Crowder Aug 09 '16 at 14:52
  • @T.J.Crowder The width won't be 0, it's setting a HTML5 canvas width. When called they can set the width or it will use the default width. – Paul Redmond Aug 09 '16 at 14:54
  • Possible duplicate of [Setting default value for TypeScript object passed as argument](http://stackoverflow.com/questions/23314806/setting-default-value-for-typescript-object-passed-as-argument) – Ricardo Rodrigues Aug 11 '16 at 09:09

0 Answers0