Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code enforces semicolons, but Angular TypeScript examples do not use consistently?

I'm taking the Microsoft Angular Fundamentals course right now and I spotted lack of semicolons in their examples, something like:

gitSearch = (query: string): Promise<GitSearch> => {
    let promise = new Promise<GitSearch>((resolve, reject) => {
        if (this.cachedValues[query]) {
            resolve(this.cachedValues[query])
        }
        else {
            this.http.get('https://api.github.com/search/repositories?q=' + query)
            .toPromise()
            .then( (response) => {
                resolve(response as GitSearch)
            }, (error) => {
                reject(error);
            })
        }
    })
    return promise;
  }

Notice they don't use semicolons after resolve.thisCachedValues[query]). But VS Code gives me TSLint warning 'Missing semicolon' at that line. Is this an issue if I omit the semicolon?

like image 925
tnsaturday Avatar asked Oct 19 '25 04:10

tnsaturday


1 Answers

Yes, it does make a difference. Typescript is an extension of Javascript and it has inherited some of it's properties. Take a look at below example:

Consider the consequences of semicolon insertion on the return statement. If a return statement returns a value, that value expression must begin on the same line as the return:

return
{
 status: true
};

This appears to return an object containing a status member. Unfortunately, semicolon insertion turns it into a statement that returns undefined. There is no warning that semicolon insertion caused the misinterpretation of the program. The problem can be avoided if the { is placed at the end of the previous line and not at the beginning of the next line:

return {
 status: true
};

So, make sure that semicolons are present at right place (mostly when a line ends in javascript). I hope this would give you some idea about the potential pitfalls if you do/do not put semicolon properly.

I have taken this example from JavaScript: The Good parts which is really a thin 172 pages but awesome book about such stuffs

like image 58
Shashank Vivek Avatar answered Oct 21 '25 17:10

Shashank Vivek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!