<p dir="auto">Currently, TypeScript requires that the <code class="notranslate">get</code> and the <code class="notranslate">set</code> types of properties match.  However, there are scenarios where, I think, it would be very useful to have a <code class="notranslate">set</code> that is more accepting than the get.</p>
<p dir="auto">For example: imagine you have a property of <code class="notranslate">Array&lt;Array&lt;string&gt;&gt;</code> type.  Today, this means that both the get and set would have to be of the same type, and so setting the property would always be done via the array notation.</p>
<p dir="auto">It seems reasonable, however, to allow the "setter" to be more forgiving and let it coalesce a single string value into an array automatically.  That way, when setting the property, you could either do <code class="notranslate">cell.values = [[1]]</code> or just <code class="notranslate">cell.values = 1</code>, and the class would accept either syntax.  But for return values, it would always return an array type, and so the <code class="notranslate">get</code> of the values property should still be marked as <code class="notranslate">Array&lt;Array&lt;string&gt;&gt;</code> (that way you would get all the good IntelliSense of having <code class="notranslate">.filter</code>, <code class="notranslate">.forEach</code>, etc., on the return type).</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class Cell {
    private _values: Array&lt;Array&lt;string&gt;&gt;;
    get values():Array&lt;Array&lt;string&gt;&gt;{
        return this._values;
    }
    set values(values: Array&lt;Array&lt;string&gt;&gt;|string) {
        if (values instanceof Array) {
            this._values = &lt;Array&lt;Array&lt;string&gt;&gt;&gt;values;
        } else {
            this._values = [[&lt;string&gt;values]];
        }
    }
}
"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-v">Cell</span> <span class="pl-kos">{</span>
    <span class="pl-c1">private</span> <span class="pl-s1">_values</span>: <span class="pl-v">Array</span><span class="pl-c1">&lt;</span><span class="pl-v">Array</span><span class="pl-c1">&lt;</span><span class="pl-s1">string</span><span class="pl-c1">&gt;&gt;</span><span class="pl-kos">;</span>
    <span class="pl-k">get</span> <span class="pl-en">values</span><span class="pl-kos">(</span><span class="pl-kos">)</span>:<span class="pl-v">Array</span><span class="pl-c1">&lt;</span><span class="pl-v">Array</span><span class="pl-c1">&lt;</span><span class="pl-s1">string</span><span class="pl-c1">&gt;&gt;</span><span class="pl-kos">{</span>
        <span class="pl-k">return</span> <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_values</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
    <span class="pl-k">set</span> <span class="pl-en">values</span><span class="pl-kos">(</span><span class="pl-s1">values</span>: <span class="pl-v">Array</span><span class="pl-c1">&lt;</span><span class="pl-v">Array</span><span class="pl-c1">&lt;</span><span class="pl-s1">string</span><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">|</span><span class="pl-s1">string</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">values</span> <span class="pl-k">instanceof</span> <span class="pl-v">Array</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
            <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_values</span> <span class="pl-c1">=</span> <span class="pl-c1">&lt;</span><span class="pl-ent">Array</span><span class="pl-c1">&lt;</span><span class="pl-c1">Array</span><span class="pl-c1">&lt;</span><span class="pl-c1">string</span><span class="pl-c1">&gt;</span><span class="pl-c1">&gt;&gt;</span><span class="pl-s1">values</span><span class="pl-kos">;</span>
        <span class="pl-kos">}</span> <span class="pl-k">else</span> <span class="pl-kos">{</span>
            <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_values</span> <span class="pl-c1">=</span> <span class="pl-kos">[</span><span class="pl-kos">[</span><span class="pl-c1">&lt;</span><span class="pl-s1">string</span><span class="pl-c1">&gt;</span>values]];
        <span class="pl-kos">}</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">Today, this code would trigger an error that "get and set accessors must have the same type".<br>
<br><br>
The only workarounds available today are both less than ideal:</p>
<ol dir="auto">
<li>
<p dir="auto">Set the property of values -- both "set" and "get" -- to <code class="notranslate">any</code>.  But in doing so, you lose all IntelliSense.</p>
</li>
<li>
<p dir="auto">Mark the property -- both "set" and "get" -- as <code class="notranslate">Array&lt;Array&lt;string&gt;&gt;|string</code>.  However, while this solves the "set" problem, the "get" property loses a lot of the IntelliSense (<code class="notranslate">.filter</code>, <code class="notranslate">.forEach</code>), and is actually no longer true to reality (the return value <em>is</em> always an array, not an "array or a string")</p>
</li>
</ol>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/2230453/8991826/f78d1cd2-36af-11e5-8acb-7f3619108997.png"><img src="https://cloud.githubusercontent.com/assets/2230453/8991826/f78d1cd2-36af-11e5-8acb-7f3619108997.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto">Would be great if the scenario of a "more inclusive <code class="notranslate">set</code> property" could be supported.</p>
<p dir="auto">Namespaces are objects that can hold types, but cannot be passed around because don't have a type themselves.<br>
Objects are mostly namespaces which can be passed around because they are typed, but cannot hold types in them.</p>
<p dir="auto">Looks like this difference is a made up thing.</p>
<p dir="auto"><strong>Consider unifying objects and namespaces.</strong></p>
<p dir="auto">possible resolution of <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="151168858" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/8308" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/8308/hovercard" href="https://github.com/microsoft/TypeScript/issues/8308">#8308</a></p>