Sunday, October 7, 2012

Typescript is neat

javascript is not picky about line endings.  Following code looks fine but causes trouble.

   1: function func() {
   2:     return 
   3:     {
   4:         greet: "Hello World"
   5:     };
   6: }
   7: console.log(func().greet);

Line number 7 prints, undefined.  This is because return statement at line number 2 has line ending.  Now if you use the first code snipped in typescript, you can avoid this error.  Typescript marks the usage of greet in line number 7 as an error.



   1: Error    1    Expected var, class, interface, or module   …\TypeScript1\TypeScript1\app.ts    7    13    app.ts

May be they can do better with the error message. 


This problem can be fixed by moving opening curly brace at line 3 to line 2.



   1: function func() {
   2:     return {
   3:         greet: "Hello World"
   4:     };
   5: }
   6: console.log(func().greet);

Now the error goes away, and also you will get intellisense  for the string property “greet” after func().  in line 6.

No comments:

Post a Comment