title: TypeScript 3.0 `unknown` type as an alternative to `any` or `generics`?
body: <p><a href="https://blogs.msdn.microsoft.com/typescript/2018/07/12/announcing-typescript-3-0-rc/#the-unknown-type" rel="noreferrer">https://blogs.msdn.microsoft.com/typescript/2018/07/12/announcing-typescript-3-0-rc/#the-unknown-type</a></p>
  <blockquote>
  <p>TypeScript 3.0 introduces a new type called <code>unknown</code> that does exactly that. Much like <code>any</code>, any value is assignable to <code>unknown</code>; however, unlike <code>any</code>, you cannot access any properties on values with the type <code>unknown</code>, nor can you call/construct them. Furthermore, values of type <code>unknown</code> can only be assigned to <code>unknown</code> or <code>any</code>.</p>
  <p>As an example, swapping the above example to use <code>unknown</code> instead of <code>any</code> forces turns all usages of foo into an error:</p>
  </blockquote>
  <pre><code>let foo: unknown = 10;
  
  // Since `foo` has type `unknown`, TypeScript
  // errors on each of these usages.
  foo.x.prop;
  foo.y.prop;
  foo.z.prop;
  foo();
  new foo();
  upperCase(foo);
  foo `hello world!`;
  
  function upperCase(x: string) {
      return x.toUpperCase();
  }
  </code></pre>
  <p>I recently started to code in TypeScript, and here is my questions:</p>
  <h3>1. It seems to me, <code>unknown</code> can be the complete alternative to <code>any</code>.</h3>
  <p>Of course, a code written in native JavaScript requires to use <code>any</code> type automatically in TypeScript, but when we start from scratch to code in pure TypeScript, is there any reason to employ <code>any</code> type instead of <code>unknown</code> from version 3.0?</p>
  <h3>2. What is the diffrence between <code>unknown</code> and <code>generics</code>?</h3>
  <p>For instance,</p>
  <pre><code>const A = &lt;T&gt;(value: T): T =&gt; value;
  const A1 = (value: unknown) =&gt; value;
  
  console.log(A&lt;string&gt;('Hello'), A&lt;number&gt;(100));
  
  
  
  const B = &lt;T, U&gt;(t: T, u: U) =&gt; {
      console.log(t);
      console.log(u);
  };
  const B1 = (t: unknown, u: unknown) =&gt; {
      console.log(t);
      console.log(u);
  };
  
  B&lt;string, number&gt;('Hello', 100);
  B&lt;number, string&gt;(100, 'Hello');
  </code></pre>
  <p>and suppose the arugument <code>value: unknown</code> is originally defintely typed, the type of the value is identified, so it seems to me there is no reason to pass <code>T</code> explicitly.</p>
  <p>Thanks.</p>