@kadikraman
Hi, my name is
I work at
... and I write a lot of JavaScript
@kadikraman
@kadikraman
@kadikraman
Flow
TypeScript
Since PEP 484
PHP 7.4
@kadikraman
Frontend
Backend
@kadikraman
"The Stack"
Frontend
Backend
π
@kadikraman
"The Stack"
Frontend
Backend
π
@kadikraman
@kadikraman
@kadikraman
function addOne(array) {
return array.map(val => val + 1);
}
addOne([1, 2, 3])
// [2, 3, 4]
addOne({})
@kadikraman
function addOne(array) {
return array.map(val => val + 1);
}
/**
* Adds 1 to each element in the array
* @param array (array of numbers): array to increment
*/
function addOne(array) {
return array.map(val => val + 1);
}
@kadikraman
Just kidding - no one reads comments
function addOne(array) {
if (!Array.isArray(array) || !array.every(val => typeof val === "number")) {
return [];
}
return array.map(val => val + 1);
}
@kadikraman
But this function is only meant to be used on a number array
function addOne(array: number[]): number[] {
return array.map(val => val + 1);
}
@kadikraman
* not valid in plain JavaScript
@kadikraman
@kadikraman
The difference is when the type checking is done!
@kadikraman
@kadikraman
Compile time vs run time type checking
@kadikraman
π€·ββοΈ Β π€·ββοΈ Β π€·ββοΈ
@kadikraman
Type errors?Β
Errors can still occur, but not type errors
@kadikraman
@kadikraman
( source: https://xkcd.com/303/Β )
@kadikraman
vs
function addOne(array: number[]): number[] {
return array.map(val => val + 1);
}
function addOne(array) {
return array.map(val => val + 1);
}
@kadikraman
@kadikraman
@kadikraman
@kadikraman
@kadikraman
@kadikraman
var myValue = 1;
myValue = "potato";
@kadikraman
@kadikraman
@kadikraman
@kadikraman
β Faster
β More flexible
Β
β Runtime type errors
β Harder to refactor
β Fewer runtime exceptions
β Fewer tests
Β
β Takes longer
β Adds verbosity
Could we borrow concepts from statically typed languages?
@kadikraman
@kadikraman
ESLint for type checking in JavaScript
@kadikraman
React Community
2 years ago
@kadikraman
@kadikraman
@kadikraman
@kadikraman
Configuration π±
Memory π
You can easily ignore it π³
@kadikraman
React Community
now
@kadikraman
Static type checker
Language
@kadikraman
@kadikraman
You can still...
@kadikraman
You can still...
@kadikraman
@kadikraman
Flow π±Β
TypeScript β
@kadikraman
Flow π±Β
TypeScript β
@kadikraman
β has a larger community
β has a faster release cycle
β is more reliable
β is easier to set up
β has an ability to enforce type-checking
Β
β although you can still ignore type checking
TypeScript...
@kadikraman
Use plain JavaScript!
@kadikraman
@kadikraman
@kadikraman
Frontend
Backend
π
@kadikraman
Frontend
Backend
(Transpiles to OCaml which compiles to JS)
let add = (x: int, y: int) : int => x + y;
@kadikraman
@kadikraman
type user = { firstName: string, lastName: string };
let user: user = {
firstName: "Harper",
lastName: "Perez"
};
let formatName user =>
user.firstName ^ " " ^ user.lastName;
let hello =
(ReactRe.stringToElement ("Hello, " ^ formatName user));
let header =
<h1 title=(formatName user)>
hello
</h1>;
ReactDOMRe.renderToElementWithId header "root";
add : Number -> Number -> Number
add a b =
a + b
(compiles to JS)
@kadikraman
import Browser
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)
main =
Browser.sandbox { init = 0, update = update, view = view }
type Msg = Increment | Decrement
update msg model =
case msg of
Increment ->
model + 1
Decrement ->
model - 1
view model =
div []
[ button [ onClick Decrement ] [ text "-" ]
, div [] [ text (String.fromInt model) ]
, button [ onClick Increment ] [ text "+" ]
]
@kadikraman
@kadikraman
1996
2014
2016
2012
JavaScript
Elm
TypeScript
Flow
Reason
(3 years)
(5 years)
(7 years)
(23 years)
OCaml
2017
(4 years)
Web Assembly
Reliability in prod
Effort to learn
JavaScript
Flow
Reason
Elm
TypeScript
@kadikraman
Reliability in prod
Effort to learn
JavaScript
Flow
Reason
Elm
TypeScript
@kadikraman
Server Fetched Partials?
π»
@kadikraman
For riding the Type Train π
@kadikraman