This is an interactive presentation. Press "s" to open the speaker view.
Hadrien Milano
Software Engineer, UI
Types + JavaScript = TypeScript
JS with types
Type aliases
Object types
Intersection (mixins)
Trick #1
Safe html strings.
Trick #1½
Union
Trick #2
Type narrowing
Trick #3
Exhaustive matching
Trick #3½
Exhaustive matching (without return type)
Bonus trick
Type-level error messages
Conditional types
A extends B ? Yes : No;
Conditional types
Conditional type inference
Let's get serious!
HTTP API definition
Not super TypeScript friendly...
What if...
Is this... magic?
No, it's TypeScript! (trick #5)
Hmmm...
Problem:
let Flatten = [A, B, C, ...] -> A | B | C | ...
First attempt
sub-problems:
Rest of a tuple
Tuple utilities (trick #4)
Tuple flattening problems
Recursive types
Trick #5: Recursive type
Putting it all together
More at https://github.com/hmil/rest.ts
TypeScript can do a lot...
But can it do everything?
What is everything?
What is everything?
Can it simulate a turing machine?
Can we implement lambda calculus in TypeScript?
Do not try this at home work.
x
var x
λx. <something>
x => <something>
<something1> <something2>
<something1>(<something2>)
Example
(λx. x) y
= y
(λx. x x) (λy. y y)
(λx=(λy. y y). x x)
= (λy. y y) (λy. y y)
Example
0 = λf. λx. x
1 = λf. λx. f x
2 = λf. λx. f (f x)
SUCC = λn. λf. λx. f ((n f) x)
SUCC 0 = (λn. λf. λx. f ((n f) x)) 0
SUCC 0 = λf. λx. f ((0 f) x)
SUCC 0 = λf. λx. f (((λf. λx. x) f) x)
SUCC 0 = λf. λx. f (( λx. x) x)
SUCC 0 = λf. λx. f x
SUCC 0 = 1
Basic model
ZERO = λf. λx. x
ONE = λf. λx. f x
TWO = λf. λx. f (f x)
Syntactic sugar
ZERO = λf. λx. x
ONE = λf. λx. f x
TWO = λf. λx. f (f x)
Reduction step
(λx. ... x ...) y -> ... y ...
Let's have fun
Church numerals (integers)
Boolean logic
We've implmented lambda-calculus in TypeScript
Therefore, TypeScript is Turing-Complete.
The end.
or is it?
Stay in touch
Thanks:
Raw clips that didn't make it to the presentation.
Conditional type inference distributivity
@(some expression which evalutates to a function)
class decorator
function MyDecorator(t: any) {
}
@MyDecorator
class MyClass {
}
or
function MyDecorator() {
return function(t: any) { }
}
@MyDecorator()
class MyClass {
}
class decorator
...or
function MyDecoratorOverkillFactory() {
return {
d: () => {
return function(t: any) { };
}
};
}
@MyDecoratorOverkillFactory().d()
class MyClass {
}
Tip: Restrict the scope of your decorator
Member decorator
Decorators can't change the type of the object they decorate...
...but they can verify it!
example:
Modelling library
Trick #xxx: Class constraint enforcement from decorator via type-level programing.
Trick #3
Type guards