<p dir="auto"><strong>TypeScript Version:</strong></p>
<p dir="auto">1.8.10</p>
<p dir="auto"><strong>Code</strong></p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="    class ClassWithStaticProp {
        static prop = {};
    }

    const instance = new ClassWithStaticProp();

    // instance.constructor has no type information
"><pre class="notranslate">    <span class="pl-k">class</span> <span class="pl-smi">ClassWithStaticProp</span> <span class="pl-kos">{</span>
        <span class="pl-k">static</span> <span class="pl-c1">prop</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>

    <span class="pl-k">const</span> <span class="pl-s1">instance</span> <span class="pl-c1">=</span> <span class="pl-k">new</span> <span class="pl-smi">ClassWithStaticProp</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

    <span class="pl-c">// instance.constructor has no type information</span></pre></div>
<p dir="auto"><strong>Expected behavior:</strong></p>
<p dir="auto"><code class="notranslate">instance.constructor</code> should have the same type as the <code class="notranslate">ClassWithStaticProp</code> constructor function.</p>
<p dir="auto"><strong>Actual behavior:</strong></p>
<p dir="auto"><code class="notranslate">instance.constructor</code> has no type information</p>
<p dir="auto">TypeScript allows to return <code class="notranslate">this</code> from instance methods, which is to say the least, is great. It would be even better if it allowed doing the same with static methods. The rationale is consistency and feature.</p>
<p dir="auto">Line <code class="notranslate">public static construct():this</code> below will report <code class="notranslate">error TS2526: A 'this' type is available only in a non-static member of a class or interface.</code>, the line below uses <code class="notranslate">this</code> inside static method to create a new instance of the class, the line below that also uses <code class="notranslate">this</code> to return static property. The error message is misleading, <code class="notranslate">this</code> can be and should be used inside static methods whenever appropriate.</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class Foo {
    public static x:any;
    protected y:any;
    public static construct():this {
        return new this();
    }
    public static getX():any {
        return this.x;
    }
    public getY():any{
        return this.y;
    }
    public setY(value:any):this{
        this.y = value;
        return this;
    }
}"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-smi">Foo</span> <span class="pl-kos">{</span>
    <span class="pl-k">public</span> <span class="pl-k">static</span> <span class="pl-c1">x</span>:<span class="pl-smi">any</span><span class="pl-kos">;</span>
    <span class="pl-k">protected</span> <span class="pl-c1">y</span>:<span class="pl-smi">any</span><span class="pl-kos">;</span>
    <span class="pl-k">public</span> <span class="pl-k">static</span> <span class="pl-en">construct</span><span class="pl-kos">(</span><span class="pl-kos">)</span>:this <span class="pl-kos">{</span>
        <span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-smi">this</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
    <span class="pl-k">public</span> <span class="pl-k">static</span> <span class="pl-en">getX</span><span class="pl-kos">(</span><span class="pl-kos">)</span>:<span class="pl-smi">any</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">x</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
    <span class="pl-k">public</span> <span class="pl-en">getY</span><span class="pl-kos">(</span><span class="pl-kos">)</span>:<span class="pl-smi">any</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">y</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
    <span class="pl-k">public</span> <span class="pl-en">setY</span><span class="pl-kos">(</span><span class="pl-s1">value</span>:<span class="pl-smi">any</span><span class="pl-kos">)</span>:this<span class="pl-kos">{</span>
        <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">value</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-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">I'd like to inherit <code class="notranslate">Foo</code> and use <code class="notranslate">construct</code> on child classes, the only way I can do this by specifying <code class="notranslate">Foo</code> as return type, however, this will require to cast the result to <code class="notranslate">Bar</code> and <code class="notranslate">Baz</code> whenever called from those classes – inconvenient and inconsistent with instance method behaviour.</p>
<p dir="auto">This seems to be common sense, since this already done for instance methods, shouldn't be much pain doing this for static methods either? What are your thoughts?</p>