Edit
This is not a duplicate. The linked question is asking why "this" is not defined implicitly by the ES6 specification. That is not what is being asked here.
The question here is why does the Typescript compiler not adjust for that fact, and more specifically, is the reason technical in nature?
End Edit
Ignoring reasons such as designer preference, is there a technical problem preventing the Typescript compiler from automatically mapping local_variable
to this.local_variable
in the below example?
import { Component, Input, OnInit } from '@angular/core';
@Component({
selector: 'app-example',
templateUrl: './app-example.component.html',
styleUrls: ['./app-example.component.css']
})
export class ExampleComponent implements OnInit {
@Input() local_variable: any;
constructor() { }
ngOnInit() {
if (local_variable) {
console.log(local_variable);
}
}
}
In a language like C++ or Java one would expect the compiler to infer the scope of local_variable
, but in Typescript the compiler throws error TS2663, complaining that it Cannot find name 'local_variable'. Did you mean the instance member 'this.local_variable'?
Why? Why are we stuck with the seemingly redundant "this"?
----
As I am unable to add another answer right now, I am going to leave the response of an individual named @webstrand to the same query posed in the Typescript gitter here in the body of the question.
webstrand @webstrand 15:29
Typescript, once you remove the type annotations, must be completely compatible with regular javascript. This is one of the project's major guidelines.
There are a few exceptions, such as enum and namespace. But they're dead simple to transpile.
So, since the underlying javascript doesn't infer this, Typescript can't either.
@l4cr0ss Historically, the reason why javascript require the seemingly redundant this is because of an old mis-feature: with.
Jefferson Hudson @l4cr0ss 15:36
Ok. I understand that, thanks. Now, you say there are a few exceptions which are allowed because they are simple to transpile. Is it the case that such resolution could be implemented as part of the transpilation process, but because of its complexity the choice is made not to? Or, would it really break something?
webstrand @webstrand 15:42
@l4cr0ss One of the features of typescript, used by some really large projects, is that it can "transpile" directly to javascript without resolving and checking all of the types. So the compiler must be able to compile to javascript without any type information at all. To correctly infer this in all cases would require that the compiler always have type information available.
webstrand @webstrand 15:43
This is a really reassuring feature to have in an ecosystem like Javascript where there's so much churn in projects. IF typescript dies in a year, I can run all of my code through the transpile process and get back pure javascript
In fact, typescript has a type-checking mode where all the type information is stored as jsdoc-style comments.
Obviously, in that case, you can't use enum or namespace either.
As a side note , use of namespace is usually frowned upon. It comes from an era before ES supported modules natively.