3

I'm a programmer in ASP.NET-MVC, and I want to add AngularJS in my projects. I really like this framework, but the problem is Visual Studio can't detect errors at the debugger level, so when I make some syntax errors I spend more time to find errors.

Is there a feature to add for helping to work with AngularJS?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yesman022
  • 65
  • 1
  • 5
  • This sounds like a feature request to the Visual Studio team, rather than a specific programming question – JAL Oct 26 '15 at 16:34
  • I would suggest you to look at [`TypeScript`](http://www.typescriptlang.org/) – Daniel J.G. Oct 26 '15 at 17:32
  • @youssefelamraoui, I made a significant edit to my answer. I see you've already accepted it, but read if you're interested. – kdbanman Oct 26 '15 at 19:01

1 Answers1

1

To answer your question directly, let's look at the de facto definition of strongly typed as it is discussed here:

In a "strongly typed" language, it is not possible for the programmer to work around the restrictions imposed by the type system. This term is almost always used to describe statically typed languages.

JavaScript is untyped. There are no restrictions imposed by the type system to even work around (until runtime). Hence, you cannot achieve "AngularJS strong typing" with vanilla JavaScript.

That said, you can use JavaScript and catch type-related errors. Augmented JS (TypeScript or Flow annotations) can achieve full static typing, and a subset of full static typing can be achieved with type inference on Vanilla JS (Flow without annotations). Read on for details.


  • Use the statically typed TypeScript. It compiles to JS and is usable within powerful IDEs like Visual Studio.

    class Person {
      private name: string;
      private age: number;
      private salary: number;
    
      constructor(name: string, age: number, salary: number) {
        this.name = name;
        this.age = age;
        this.salary = salary;
      }
    
      toString(): string {
        return `${this.name} (${this.age}) (${this.salary})`; // As of version 1.4
      }
    }
    

  • Use an analysis tool like Flow. Flow will analyze vanilla JS for obvious type errors (misuses of inferred types):

    /* @flow */
    
    function foo(x) {
      return x * 10;
    }
    
    foo('Hello, world!');
    

    throws

    7: foo("Hello, world!");
       ^^^^^^^^^^^^^^^^^^^^ function call
    4:   return x*10;
                ^ string. This type is incompatible with
    4:   return x*10;
                ^^^^ number
    

    and you can use annotations to augment vanilla JS for more powerful type checking:

    /* @flow */
    
    function foo(x: string, y: number): string {
      return x.length * y;
    }
    
    foo('Hello', 42);
    

    throws

    4:   return x.length * y;
                ^^^^^^^^^^^^ number. This type is incompatible with
    3: function foo(x: string, y: number): string {
                                           ^^^^^^ string
    
Community
  • 1
  • 1
kdbanman
  • 10,161
  • 10
  • 46
  • 78