Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type checker for JavaScript?

Does anyone know if there's a good tool for analyzing JavaScript code and detecting type errors? I know that JavaScript itself is weakly and dynamically typed, but it would be really nice if I could have a program that would verify that all my field accesses are sensible and that I don't try treating a number like a string, for example. I'm aware that there are valid use cases in JavaScript where adding or removing fields or converting between different types is valid and expected, but some errors are so blatant it seems like they could be caught before the script started running.

I've heard of JSLint, but my understanding is that it's mostly a style tool rather than a semantic analyzer. If I'm wrong about this, then just telling me so would be a valid answer to this question.

like image 416
templatetypedef Avatar asked Feb 08 '11 05:02

templatetypedef


People also ask

Does JavaScript have static type checking?

In the JavaScript community, Flow and TypeScript have emerged as the two main options for enabling static type checking: Flow is a static type checker for JavaScript. It is an open-source tool developed by Facebook. TypeScript is a statically typed superset of JavaScript that compiles to plain JavaScript.

What is a type checker?

Type checking is the process of verifying and enforcing constraints of types in values. A compiler must check that the source program should follow the syntactic and semantic conventions of the source language and it should also check the type rules of the language.

What is @flow in JavaScript?

Flow is a static type checker that allows a developer to check for type errors while developing code. This means a developer receives faster feedback about the code, which they can use to improve its quality. Flow works by using annotations and type definitions for adding type checking support to your code.

Which is better Flow or TypeScript?

In general, TypeScript is faster than Flow when it comes to performance. Flow is quicker to start using in a project, as it only requires you to add an annotation to a regular JavaScript file. For efficiency, TypeScript is the best choice.


1 Answers

Google closure compiler does some type checking if you follow their recommendations for annotating your functions with jsDoc comments. See http://code.google.com/closure/compiler/docs/js-for-compiler.html

like image 63
Chetan S Avatar answered Sep 20 '22 03:09

Chetan S