YES
OF COURSE
duh!
done at compile-time
predictable type validity
machine-code optimizations
compiler instructions ?
done at run-time
unpredictable type validity
variables can change types
program feature ?
vs
typing
typing
JavaScript has dynamic typing: a variable is not bound to a type
NOPE
var hi = "Hello !";
hi = 83110;
hi = ["H","e","l","l",0] ;
hi = function(){ alert("Hello !"); };
There is a never-ending argument about the definition of "strong" type-checking, but here are two common definitions:
1. the ability to bind an explicit type to a variable and guarantee that this type always describe correctly the referenced data.
2. no lack of type safety due to looser typing rules,
for example implicit type coercion
HUM...
Allowed in JS, forbidden in Python
Examples of
implicit boolean coercions
Loose equality: == vs ===
Variable reassignment
const has become the
common recommended way
of declaring variables
Immutability is now highly valued
in JavaScript community
NO, but...
maybe we can do something about it ?
You miss the point of static typing !
1. the ability to bind an explicit type to a variable and guarantee that this type always describe correctly the referenced data.
Strong Typing
→ obvious solution: static type annotations
You can/must declare a variable with its type
Problem solved !
Actually, type-checking is not necessarily
the most interesting aspect of static type annotations
JSDoc
Statically analyzable by most IDE, but a bit limited
We're in the era of compilers and front-end build tooling
Superset of JS = ascending compat = progressively adoptable
Ease of adoption is a huge popularity factor
But beware of the superset myth:
JS and TS evolve in parallel,
and the already existing deviations
ill widen over time.
example: private fields are coming to JS
what about private keyword in TS ?
TypeScript real power:
static code analysis and type inference inside the IDE
The main argument in favor of TypeScript is the huge step up for Developer eXperience
Warning, personal opinion ahead !
for TypeScript adoption
Definition files are *.d.ts files that allow you to provide type information for JavaScript code that is not statically typed.
Useful to add type annotations
to existing JS libs
Since TS 3.7, automatically generate .d.ts from JS files !
NO, but...
maybe we can do something about it ?
JavaScript has been designed for scripts and beginners:
JS, why don't you have static typing ?
Dynamic languages are popular in large part because programmers can keep types latent in the code, with type checking done imperfectly (yet often more quickly and expressively) in the programmers’ heads and unit tests, and therefore programmers can do more with less code
I am not religious. I use static and dynamic languages all the time — and if there is one thing I’ve learned as a programmer,
it is that there is never one right language for all jobs.
Unpredictable stuff for a front-end web dev:
(unpredictable = known at run-time)
Server responses
Browser support
(& bugs)
Client-side stored data
Platform
features
User inputs
This is data validation, not type-checking
TypeScript can't help you with that
How do I type-check all of this,
please ?
But it's basically the same problem...
Sorry, not a compiler problem,
more an applicative problem
You'll have to deal with it by yourself
if(!isValid(data)){ /* try to fix stuff */ }
try { } catch(error){ alert(`Something bad happened: ${error}`) }
window.onerror = pretendWeCareAboutUnexpectedErrors
☠
All these dedicated tools are basically doing runtime validation
the ability to bind an explicit type to a variable and guarantee that this type always describe correctly the referenced data.
Strong Typing
→ what if we could do strong dynamic typing ?
Strong Dynamically Typed Object Modeling for JavaScript
the ability to bind an explicit type to a variable and guarantee that this type always describe correctly the referenced data.
Strong Typing
See http://objectmodel.js.org/ docs for more information
Require browser support for ES6 Proxies:
(Edge 14+, Firefox 47+, Chrome 50+, Safari 10+, Node 6.0+)
NO, but...
maybe we can do something about it ?
USE BOTH
Recommendation:
Static anotations
in core logic
Dynamic modeling
on external interfaces
they address different issues
thanks !