<p dir="auto"><strong>TypeScript Version:</strong></p>
<p dir="auto">nightly</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 A {
    get a(): this is A {
        return true
    }
}"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-smi">A</span> <span class="pl-kos">{</span>
    <span class="pl-k">get</span> <span class="pl-en">a</span><span class="pl-kos">(</span><span class="pl-kos">)</span>: <span class="pl-smi">this</span> is <span class="pl-smi">A</span> <span class="pl-kos">{</span>
        <span class="pl-k">return</span> <span class="pl-c1">true</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto"><strong>Expected behavior:</strong></p>
<p dir="auto">The getter type guard should not complain</p>
<p dir="auto"><strong>Actual behavior:</strong></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="tsc
Library/DataStore.ts(55,27): error TS1228: A type predicate is only allowed in return type position for functions and methods.
Library/DataStore.ts(103,20): error TS1228: A type predicate is only allowed in return type position for functions and methods."><pre class="notranslate"><code class="notranslate">tsc
Library/DataStore.ts(55,27): error TS1228: A type predicate is only allowed in return type position for functions and methods.
Library/DataStore.ts(103,20): error TS1228: A type predicate is only allowed in return type position for functions and methods.
</code></pre></div>
<p dir="auto">Angular/Typescript teams discussed this in person.</p>
<p dir="auto">Angular 2 includes a template parser, and an expression language which may be used in places in the template.<br>
We would like to translate the template to a TypeScript buffer, and pass the buffer to the language services for things like producing semantic errors, requesting intellisense, and the other usual editor features.<br>
However, the template has a backing class (actually a virtual class per <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/tbosch/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/tbosch">@tbosch</a> ) where the fields may be referenced by template expressions. In our canonical example,</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="@Component({selector: 'greet', template: 'Hello {{name}}!'})
 class Greet {
   name: string;
   constructor() {
     this.name = 'World';
   }
 }"><pre class="notranslate"><code class="notranslate">@Component({selector: 'greet', template: 'Hello {{name}}!'})
 class Greet {
   name: string;
   constructor() {
     this.name = 'World';
   }
 }
</code></pre></div>
<p dir="auto">The template could be represented in TypeScript with the expression</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="`Hello ${this.name}`"><pre class="notranslate"><code class="notranslate">`Hello ${this.name}`
</code></pre></div>
<p dir="auto">In order to get intellisense or errors for that expression, we need to evaluate it in a place where <code class="notranslate">this.name</code> is defined. The obvious way to do this is for us to pass a "detached buffer", meaning a range of the SourceFile which lives outside the file content. It could look like (just a wild stab at possible syntax):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="__template_eval(): string { return `Hello ${this.name}`; }"><pre class="notranslate"><code class="notranslate">__template_eval(): string { return `Hello ${this.name}`; }
</code></pre></div>
<p dir="auto">as if this code existed inside the <code class="notranslate">Greet</code> class.</p>
<p dir="auto">Also we would expect any ranges in the result to give the <code class="notranslate">pos</code> and <code class="notranslate">width</code> locations relative to the buffer we passed, not the <code class="notranslate">Greet</code> class.</p>