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.

3
  • options.width = options.width || 600 What if the width is supposed to be 0? Commented Aug 9, 2016 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. Commented Aug 9, 2016 at 14:54
  • Possible duplicate of Setting default value for TypeScript object passed as argument Commented Aug 11, 2016 at 9:09

0

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.