title: Force assignment in TypeScript?
body: <p>For example I want a function that takes an object of numbers and replaces each number with its stringified value. In JavaScript i could write something like this:</p>
  
  <pre><code>function myStringifier(object) {
    for (let key in object) {
      object[key] = object[key].toString();
    }
  }
  
  myObject = {
    a: 1,
    b: 2,
    c: 3
  };
  
  myStringifier(myObject);
  </code></pre>
  
  <p>But how can I handle this in TypeScript? The following code would produce an error:</p>
  
  <pre><code>function myStringifier(object : {[index: string]: number}) : void {
    for (let key in object) {
      object[key] = object[key].toString(); /* ERROR! Object properties are of type number 
      and therefore cannot be assigned any string values to */
    }
  }
  </code></pre>
  
  <p>Important note: I`d like to do this <strong>WITHOUT</strong> allocating any additional variables inside the function scope and/or copying the object or reference to it.</p>
  
  <p><strong>P.S.:</strong> setting the type of the object argument to:</p>
  
  <pre><code>{[index: string]: number | string}
  </code></pre>
  
  <p>- is not the way out either. I want to be sure that that the argument passed is an object of numbers.</p>
  
  <p><strong>P.P.S.:</strong></p>
  
  <p>Thanks for your answers.</p>
  
  <p>I understand that the requirements may seem a bit wired, so I probably should have provided the real case instead of an abstract example.</p>
  
  <p>I have a function that takes a few arguments, one of them is an object of additional parameters:</p>
  
  <pre><code>function setCookie(name, value, {path, domain, expires}) {...}
  </code></pre>
  
  <p>Parameter <strong>expires</strong> was meant to have value of type <strong>Date</strong>, so the JavaScript implementation of that function could just transform this one field on the <strong>options</strong> object into a UTCString, then just take the whole <strong>options</strong> object, parse it and append to the cookie string.</p>
  
  <p>The <strong>options</strong> object except that one field is ready-to-use, so allocating another variable for it just to meet the requirements of TypeScript seemed a little bit annoying.</p>
  
  <p>It looks like there are three ways out, but each of them has its own limitations:</p>
  
  <ol>
  <li>"Dirty hack" style.</li>
  </ol>
  
  <p>The first way is to use type casting:</p>
  
  <pre><code>type cookieOptions = {
    domain?: string, 
    path? : string,
    expires?: Date
  };
  
  function setCookie(name : string, value : string, options : cookieOptions) {
     if ("expires" in options) {
       options.expires = options.expires.toUTCString() as any;  // type casting
     }
     for (let propName in options) {
       updatedCookie += `; ${propName}=${options[propName] as any}`; // type casting
     }
  }
  </code></pre>
  
  <p>This concern, firstly, forces us to use type casting twice (while setting the property to the formatted value and while parsing the options object), and secondly, it violates the principle of type-safety, which crushes all benefits from using TypeScript.</p>
  
  <ol start="2">
  <li>"Ok I`l handle this" style.</li>
  </ol>
  
  <p>We could just accept formatted string as a possible value of the "expires" param. But that means that we also have to change the implementation.</p>
  
  <pre><code>type ExtendedCookieOptions = {
    domain?: string, 
    path? : string,
    expires?: string | Date
  };
  
  function setCookie(name : string, value : string, options : ExtendedCookieOptions) {...}
  </code></pre>
  
  <ol start="3">
  <li><p>"Ok, let`s allocate a variable and use it" style described by <a href="https://stackoverflow.com/a/41645100/6465846">Lodewijk</a>.</p>
  
  <p>function setCookie(name : string, value : string, options : cookieOptions) {
    let params : {[index: string] : string} = options as any; // after that, we forget about the "options" variable and use "params" instead
  }</p></li>
  </ol>
  
  <p>Again, here we have to perform an operation that seems absolutely useless in JavaScript, just to meet the requirements of TypeScript, and that<code>s why I didn</code>t want to add a variable, but in this case it seems to be the best way out.</p>