Type systems are associated with most programmers with something really hard, strongly academic and difficult to understand, and not useful in the daily life of the developer. I will try to change that, at least partly. While I do not believe that a great knowledge of type systems will make you much more productive, I will try to dispel the myth that it is something difficult and incomprehensible. In an accessible and interactive way, I will introduce a little "Computer Science" which can help us understand why some of the languages we use are designed in a way that's different.
First of all, we will answer the truth - how much "Typed" TypeScript really is?
1. Ten Typ tak ma
O systemach typów na przykładzie TypeScript’a
Artur Skowroński
#3
2. T E N TY P TA K M A
O S Y S T E M A C H T Y P Ó W N A P R Z Y K Ł A D Z I E
T Y P E S C R I P T ’ A
A R T U R S KO W R O Ń S K I
3. T H E R E A R E T W O H A R D T H I N G S
I N C O M P U T E R S C I E N C E :
P H I L K A R LT O N
C A C H E I N VA L I D AT I O N
N A M I N G T H I N G S
4. J E F F AT W O O D
T H E R E A R E T W O H A R D T H I N G S
I N C O M P U T E R S C I E N C E :
C A C H E I N VA L I D AT I O N
O F F - B Y- O N E E R R O R S
N A M I N G T H I N G S
5. J E F F AT W O O D
T H E R E A R E T W O H A R D T H I N G S
I N C O M P U T E R S C I E N C E :
C A C H E I N VA L I D AT I O N
O F F - B Y- O N E E R R O R S
N A M I N G T H I N G S
8. O N T O L O G Y
" O N T O " O Z N A C Z A „ T O , C O J E S T ” , „ C O K O LW I E K ”
9. W H AT A R E T H E M E A N I N G S O F B E I N G ?
10. W H AT I S A T H I N G ?
W H AT A R E T H E VA R I O U S M O D E S O F B E I N G O F
E N T I T I E S ?
I N T O W H AT C AT E G O R I E S , I F A N Y, C A N W E S O R T
E X I S T I N G T H I N G S ?
11.
12. I N T O W H AT C AT E G O R I E S , I F A N Y, C A N W E S O R T
E X I S T I N G T H I N G S ?
35. A N Y F O O L C A N W R I T E C O D E
T H AT A C O M P U T E R C A N
U N D E R S TA N D . G O O D
P R O G R A M M E R S W R I T E C O D E
T H AT H U M A N S C A N
U N D E R S TA N D .
M A R T I N F O W L E R
66. S TAT I C / D Y N A M I C
RUNTIME
String.trim()
value = 1
type = int
variable
Compiler &
Type Checker
SOURCE
CODE
67. S M A L L F L E X I B I L I T Y / R U N T I M E E R R O R S
RUNTIME
String.trim()
value = 1
type = int
variable
Compiler &
Type Checker
SOURCE
CODE
74. N O M I N A L T Y P I N G
class Foo {
method(input: string): number { ... }
}
class Bar {
method(input: string): number { ... }
}
let foo: Foo = new Bar();
75. N O M I N A L T Y P I N G
class Foo {
method(input: string): number { ... }
}
class Bar {
method(input: string): number { ... }
}
let foo: Foo = new Bar();
76. N O M I N A L T Y P I N G
class Foo {
method(input: string): number { ... }
}
class Bar {
method(input: string): number { ... }
}
let foo: Foo = new Bar();
77. S T R U C T U R A L T Y P I N G
class Foo {
method(input: string): number { ... }
}
class Bar {
method(input: string): number { ... }
}
let foo: Foo = new Bar();
78. S T R U C T U R A L T Y P I N G
class Foo {
method(input: string): number { ... }
}
class Bar {
method(input: string): number { ... }
}
let foo: Foo = new Bar();
96. INTERSECTIONS
interface Person {
age: number;
}
interface RecruitmentProcess {
recruitmentId: string;
}
type interfaceGuest = Person & RecruitmentProcess
let paul: interviewGuest
paul.recruitmentId = 'ID3241'
paul.age = 24
97. INTERSECTIONS
interface Person {
age: number;
}
interface RecruitmentProcess {
processId: string;
}
type interfaceGuest = Person & RecruitmentProcess
let paul: interviewGuest
paul.recruitmentId = 'ID3241'
paul.age = 24
98. INTERSECTIONS
interface Person {
age: number;
}
interface RecruitmentProcess {
processId: string;
}
type interfaceGuest = Person & RecruitmentProcess
let paul: interviewGuest
paul.recruitmentId = 'ID3241'
paul.age = 24
99. INTERSECTIONS
interface Person {
age: number;
}
interface RecruitmentProcess {
processId: string;
}
type interfaceGuest = Person & RecruitmentProcess
let paul: interviewGuest
paul.processId = 'ID3241'
paul.age = 24
100. PA R A M E T R I Z E D P O LY M O R P H I S M
101. E X . A R R AY S
let message: Array<String> = []
message.push("test");
message.push(2);
102. E X . A R R AY S
let message: Array<String> = []
message.push("test");
message.push(2);
103. E X . A R R AY S
let message: Array<String> = []
message.push("test");
message.push(2);
104. E X . A R R AY S
let message: Array<String> = []
message.push("test");
message.push(2);
105. E X . A R R AY S
let message: Array<String> = []
message.push("test");
message.push(2);
106. E X . A R R AY S
function unsafeAdd(arr: Array<string | number>): void {
arr.push(3)
}
let message: Array<String> = [ 'first' ]
unsafeAdd(message)
const el: string = message[1]
console.log(el.toLowerCase())
107. U N S O U N D T Y P E S
function unsafeAdd(arr: Array<string | number>): void {
arr.push(3)
}
let message: Array<String> = [ 'first' ]
unsafeAdd(message)
const el: string = message[1]
console.log(el.toLowerCase())
108. U N S O U N D T Y P E S
function unsafeAdd(arr: Array<string | number>): void {
arr.push(3)
}
let message: Array<String> = [ 'first' ]
unsafeAdd(message)
const el: string = message[1]
console.log(el.toLowerCase())
109. U N S O U N D T Y P E S
function unsafeAdd(arr: Array<string | number>): void {
arr.push(3)
}
let message: Array<String> = [ 'first' ]
unsafeAdd(message)
const el: string = message[1]
console.log(el.toLowerCase())
110. U N S O U N D T Y P E S
function unsafeAdd(arr: Array<string | number>): void {
arr.push(3)
}
let message: Array<String> = [ 'first' ]
unsafeAdd(message)
const el: string = message[1]
console.log(el.toLowerCase())
111. U N S O U N D T Y P E S
function unsafeAdd(arr: Array<string | number>): void {
arr.push(3)
}
let message: Array<String> = [ 'first' ]
unsafeAdd(message)
const el: string = message[1]
console.log(el.toLowerCase())
112. U N S O U N D T Y P E S
function unsafeAdd(arr: Array<string | number>): void {
arr.push(3)
}
let message: Array<String> = [ 'first' ]
unsafeAdd(message)
const el: string = message[1]
console.log(el.toLowerCase())
113. U N S O U N D T Y P E S
function unsafeAdd(arr: Array<string | number>): void {
arr.push(3)
}
let message: Array<String> = [ 'first' ]
unsafeAdd(message)
const el: string = message[1]
console.log(el.toLowerCase())
114. U N S O U N D T Y P E S
function unsafeAdd(arr: Array<string | number>): void {
arr.push(3)
}
let message: Array<String> = [ 'first' ]
unsafeAdd(message)
const el: string = message[1]
console.log(el.toLowerCase())
115. U N S O U N D T Y P E S
function unsafeAdd(arr: Array<string | number>): void {
arr.push(3)
}
let message: Array<String> = [ 'first' ]
unsafeAdd(message)
const el: string = message[1]
console.log(el.toLowerCase())
116. VA L I D J S C O D E
function unsafeAdd(arr) {
arr.push(3)
}
var message: Array<String> = [ 'first' ]
unsafeAdd(message)
var el = message[1]
console.log(el.toLowerCase())
118. H O W T O M A K E I T S O U N D
function checkIfTypeOk(el, type) {
if(!el instanceof type) throw new ClassCastException()
}
var message: Array<String> = [ 'first' ]
unsafeAdd(message)
var el = message[1]
checkIfTypeOk(el, string)
console.log(el.toLowerCase())
119. H O W T O M A K E I T S O U N D
function checkIfTypeOk(el, type) {
if(!el instanceof type) throw new ClassCastException()
}
var message: Array<String> = [ 'first' ]
unsafeAdd(message)
var el = message[1]
checkIfTypeOk(el, string)
console.log(el.toLowerCase())
120. H O W T O M A K E I T S O U N D
function checkIfTypeOk(el, type) {
if(!el instanceof type) throw new ClassCastException()
}
var message: Array<String> = [ 'first' ]
unsafeAdd(message)
var el = message[1]
checkIfTypeOk(el, string)
console.log(el.toLowerCase())
121. H O W T O M A K E I T S O U N D
function checkIfTypeOk(el, type) {
if(!el instanceof type) throw new ClassCastException()
}
var message: Array<String> = [ 'first' ]
unsafeAdd(message)
var el = message[1]
checkIfTypeOk(el, string)
console.log(el.toLowerCase())
122. T R A D E O F F : E A S Y T O U S E / C O R R E C T
C O M PAT I B L E W I T H J S
123. GRADUAL (STATIC WITH TYPE ERASURE)
STRUCTURALLY TYPED WITH TYPE WIDENING UNSOUND
124. S O U R C E S
https://www2.ccs.neu.edu/racket/pubs/ecoop2015-takikawa-et-al.pdf
https://users.soe.ucsc.edu/~abadi/Papers/FTS-submitted.pdf
UNDERSTANDING TYPESCRIPT
https://blog.steveklabnik.com/posts/2010-07-17-what-to-know-before-debating-type-systems
WHAT TO KNOW BEFORE DEBATING TYPE SYSTEMS
TOWARDS PRACTICAL GRADUAL TYPING
https://papl.cs.brown.edu/2014/safety-soundness.html
SAFETY AND SOUNDNESS