title: Which of the following two methods do you use to convert untyped values to typed values at the boundaries?
body: <p>In languages like python and typescript where there is an <code>any</code> type, database query() functions return <code>object</code>s/<code>dict</code>s with <code>any</code> type.</p>
  <p>When your column names/field names are same in both the database, would you do:</p>
  <pre class="lang-js prettyprint-override"><code>type Foo {
    foo: string,
    bar: string
  }
  
  // Method 1.
  function foo1(): Foo {
      let row = queryDB() // row: any
      return row
  }
  </code></pre>
  <p>or</p>
  <pre class="lang-js prettyprint-override"><code>// Method 2.
  function foo2(): Foo {
      row = queryDB() // row: any
      let ret: Foo = {
         foo: row['foo'],
         bar: row['bar']
      }
      return ret;
  }
  </code></pre>
  <p>I have seen codebases doing both of these but I am not sure of the maintainability, correctness or performance implications of these.</p>
  <p>For example, it can be imagined that you renamed the column <code>bar</code> to <code>baz</code> in the database, and forgot to update the code.</p>
  <p>In the case of <code>foo1</code>, the error will be raised in some remote part of the code which tried to access <code>bar</code> and you have no idea why <code>bar</code> is undefined. Whereas in the case of <code>foo2</code> the error will be raised in line 4 of <code>foo2</code>.</p>
  <p>On the other hand, <code>foo2</code> is wasting cycles shallow cloning the row.</p>
  <p>And if you add a column to Foo table, there are now three places to update in the case of <code>foo2</code>: DB schema, <code>type Foo</code> and <code>Foo = {..}</code> in <code>foo2</code>.
  Whereas there are only two places to update in <code>foo1</code>.</p>
  <p>What would an experienced developer do in cases like this? and why?</p>