<p dir="auto">Right now interfaces use the same syntax for properties with and without accessors. This is fine for duck typed interfaces, but the problem is, there is no way to specify properties with only one part (getter or setter) present. Usually, they are read only, without setter. The problem is, we can't tell this to a compiler, so it will not warn us when assigning to such a prop and JS runtime will simply ignore this assignment without throwing.</p>
<p dir="auto">It would be good to have syntax like this</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="interface IEmployee{
     get fullName: string;
     set firstName: string;
     set lastName: string; 
}"><pre class="notranslate"><span class="pl-s1">interface</span> <span class="pl-v">IEmployee</span><span class="pl-kos">{</span>
     get <span class="pl-c1">fullName</span>: <span class="pl-s1">string</span><span class="pl-kos">;</span>
     set <span class="pl-s1">firstName</span>: <span class="pl-s1">string</span><span class="pl-kos">;</span>
     set <span class="pl-s1">lastName</span>: <span class="pl-s1">string</span><span class="pl-kos">;</span> 
<span class="pl-kos">}</span></pre></div>
<p dir="auto">to prevent such errors.</p>
<p dir="auto">Some properties in JavaScript are actually read-only, i.e. writes to them either fail silently or cause an exception. These should be modelable in TypeScript.</p>
<p dir="auto">Previous attempts to design this have run into problems. A brief exploration:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="interface Point {
    x: number;
    y: number;
}
interface ImmutablePoint {
    readonly x: number;
    readonly y: number;
}
var pt: ImmutablePoint = { x: 4, y: 5 }; // OK, can convert mutable to non-mutable
pt.x = 5; // Error, 'pt.x' is not a valid target of assignment

var pt2: Point = pt; // Error, cannot convert readonly 'x' to mutable 'x'

// Possibly bad behavior
var pt3: Point = { x: 1, y: 1 };
var pt4: ImmutablePoint = pt3; // OK
pt3.x = 5; // pt4.x is also changed?

// Really bad behavior
/** This function was written in TypeScript 1.0 **/
function magnitudeSquared(v: { x: number; y: number }) {
   return v.x * v.x + v.y * v.y;
}
// Now try to use it with ImmutablePoint
console.log(magnitudeSquared(pt)); // Error, cannot use readonly object in non-readonly call"><pre class="notranslate"><span class="pl-k">interface</span> <span class="pl-smi">Point</span> <span class="pl-kos">{</span>
    <span class="pl-c1">x</span>: <span class="pl-smi">number</span><span class="pl-kos">;</span>
    <span class="pl-c1">y</span>: <span class="pl-smi">number</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>
<span class="pl-k">interface</span> <span class="pl-smi">ImmutablePoint</span> <span class="pl-kos">{</span>
    <span class="pl-k">readonly</span> <span class="pl-c1">x</span>: <span class="pl-smi">number</span><span class="pl-kos">;</span>
    <span class="pl-k">readonly</span> <span class="pl-c1">y</span>: <span class="pl-smi">number</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>
<span class="pl-k">var</span> <span class="pl-s1">pt</span>: <span class="pl-smi">ImmutablePoint</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span> <span class="pl-c1">x</span>: <span class="pl-c1">4</span><span class="pl-kos">,</span> <span class="pl-c1">y</span>: <span class="pl-c1">5</span> <span class="pl-kos">}</span><span class="pl-kos">;</span> <span class="pl-c">// OK, can convert mutable to non-mutable</span>
<span class="pl-s1">pt</span><span class="pl-kos">.</span><span class="pl-c1">x</span> <span class="pl-c1">=</span> <span class="pl-c1">5</span><span class="pl-kos">;</span> <span class="pl-c">// Error, 'pt.x' is not a valid target of assignment</span>

<span class="pl-k">var</span> <span class="pl-s1">pt2</span>: <span class="pl-smi">Point</span> <span class="pl-c1">=</span> <span class="pl-s1">pt</span><span class="pl-kos">;</span> <span class="pl-c">// Error, cannot convert readonly 'x' to mutable 'x'</span>

<span class="pl-c">// Possibly bad behavior</span>
<span class="pl-k">var</span> <span class="pl-s1">pt3</span>: <span class="pl-smi">Point</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span> <span class="pl-c1">x</span>: <span class="pl-c1">1</span><span class="pl-kos">,</span> <span class="pl-c1">y</span>: <span class="pl-c1">1</span> <span class="pl-kos">}</span><span class="pl-kos">;</span>
<span class="pl-k">var</span> <span class="pl-s1">pt4</span>: <span class="pl-smi">ImmutablePoint</span> <span class="pl-c1">=</span> <span class="pl-s1">pt3</span><span class="pl-kos">;</span> <span class="pl-c">// OK</span>
<span class="pl-s1">pt3</span><span class="pl-kos">.</span><span class="pl-c1">x</span> <span class="pl-c1">=</span> <span class="pl-c1">5</span><span class="pl-kos">;</span> <span class="pl-c">// pt4.x is also changed?</span>

<span class="pl-c">// Really bad behavior</span>
<span class="pl-c">/** This function was written in TypeScript 1.0 **/</span>
<span class="pl-k">function</span> <span class="pl-en">magnitudeSquared</span><span class="pl-kos">(</span><span class="pl-s1">v</span>: <span class="pl-kos">{</span> <span class="pl-c1">x</span>: <span class="pl-smi">number</span><span class="pl-kos">;</span> <span class="pl-c1">y</span>: <span class="pl-smi">number</span> <span class="pl-kos">}</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
   <span class="pl-k">return</span> <span class="pl-s1">v</span><span class="pl-kos">.</span><span class="pl-c1">x</span> <span class="pl-c1">*</span> <span class="pl-s1">v</span><span class="pl-kos">.</span><span class="pl-c1">x</span> <span class="pl-c1">+</span> <span class="pl-s1">v</span><span class="pl-kos">.</span><span class="pl-c1">y</span> <span class="pl-c1">*</span> <span class="pl-s1">v</span><span class="pl-kos">.</span><span class="pl-c1">y</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>
<span class="pl-c">// Now try to use it with ImmutablePoint</span>
<span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-en">magnitudeSquared</span><span class="pl-kos">(</span><span class="pl-s1">pt</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// Error, cannot use readonly object in non-readonly call</span></pre></div>
<p dir="auto">Possible solutions?</p>
<ul dir="auto">
<li>Allow bivariance of mutability: this is very unsound</li>
<li>Something else clever? C++ did not do well with const contamination</li>
</ul>