What is TypeScript?
JS + Static Types
- Not having to worry about using properties wrongly
- Compile-Time checking
- Better Autocompletion
- Types exist at compile time only
| const div = (a, b) => a / b; |
| |
| div(4, 2) === 2 |
| div(4, "abc") === NaN |
| const div = (a: number, b: number): number => a / b; |
| |
| div(4, 2) === 2 |
| div(4, "abc") |
| const div = (a: number, b: number) => a / b; |
| |
| div(4, 2) === 2 |
| div(4, "abc") |
JS ⊆ TS
➡️ gradual adoption
Simple Types
number
string
boolean
- Literals
true
& false
- String literals (like
"bla"
)
- Number literals (like
1
, 2
)
Records & Interfaces
| interface SomeType { |
| member1: boolean; |
| member2: HTMLElement; |
| member3: string; |
| member4: "bla"; |
| } |
Records & Interfaces
| interface SomeType { |
| member1: boolean; |
| member2: HTMLElement; |
| member3: string; |
| member4: "bla"; |
| } |
Union types
"This variable has all members of both types"
TypeA & TypeB
, "TypeA
and TypeB
"
| const a = { x: 123 }; |
| const b = { y: 456 }; |
| |
| |
| |
| const a = { x: 123 }; |
| const b = { y: 456 }; |
| |
| |
| const aAndB = { ...a, ...b }; |
| const a = { x: 123 }; |
| const b = { y: 456 }; |
| |
| |
| const aAndB = { ...a, ...b }; |
Intersection types
"This variable is either of type A or of type B"
TypeA | TypeB
, "TypeA
or TypeB
"
| |
| |
| |
| |
| const select = () => { |
| |
| const a = { variant: 'A', x: 123 }; |
| |
| const b = { variant: 'B', y: 456 }; |
| |
| |
| }; |
| |
| |
| |
| |
| const select = () => { |
| |
| const a = { variant: 'A', x: 123 }; |
| |
| const b = { variant: 'B', y: 456 }; |
| |
| |
| }; |
| |
| |
| |
| |
| const select = () => { |
| |
| const a = { variant: 'A', x: 123 }; |
| |
| const b = { variant: 'B', y: 456 }; |
| |
| return Math.random() > 0.5 ? a : b; |
| }; |
| |
| |
| |
| |
| const select = () => { |
| |
| const a = { variant: 'A', x: 123 }; |
| |
| const b = { variant: 'B', y: 456 }; |
| |
| return Math.random() > 0.5 ? a : b; |
| }; |
Intersection narrowing
| type A = typeof a = { variant: 'A', x: number }; |
| |
| type B = typeof b = { variant: 'B', y: number }; |
| const getStr = (item: A | B): string => { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| }; |
| const getStr = (item: A | B): string => { |
| switch (item.variant) { |
| case "A": |
| |
| |
| |
| |
| case "B": |
| |
| |
| |
| |
| |
| |
| } |
| }; |
| const getStr = (item: A | B): string => { |
| switch (item.variant) { |
| case "A": |
| console.log(item.x); |
| |
| |
| |
| case "B": |
| |
| |
| |
| |
| |
| |
| } |
| }; |
| const getStr = (item: A | B): string => { |
| switch (item.variant) { |
| case "A": |
| console.log(item.x); |
| |
| |
| |
| case "B": |
| console.log(item.y); |
| |
| |
| |
| |
| |
| } |
| }; |
| const getStr = (item: A | B): string => { |
| switch (item.variant) { |
| case "A": |
| console.log(item.x); |
| console.log(item.y); |
| |
| |
| case "B": |
| console.log(item.y); |
| console.log(item.x); |
| |
| |
| |
| |
| } |
| }; |
| const getStr = (item: A | B): string => { |
| switch (item.variant) { |
| case "A": |
| console.log(item.x); |
| console.log(item.y); |
| return String(item.x); |
| |
| case "B": |
| console.log(item.y); |
| console.log(item.x); |
| return String(item.y); |
| |
| |
| |
| } |
| }; |
| const getStr = (item: A | B): string => { |
| switch (item.variant) { |
| case "A": |
| console.log(item.x); |
| console.log(item.y); |
| return String(item.x); |
| |
| case "B": |
| console.log(item.y); |
| console.log(item.x); |
| return String(item.y); |
| |
| |
| |
| } |
| }; |
Strict Mode
tsc
can treat null
and undefined
as concrete types
- Every other type then becomes non-nullable
- Very powerful when combined with intersection types
- Forces programmer to handle
null
-case
- Completely eliminates
null
-errors 🎉
| const trim = (param: string | null) => { |
| return param.trim(); |
| |
| if (!param) { |
| |
| return ''; |
| } |
| |
| return param.trim(); |
| }; |
TypeScript + React?
- Safe passing of props
- Handle state correctly
Creating a new React App in TypeScript
$ npx create-react-app my-app --typescript
$ yarn create react-app my-app --typescript
Conclusion
- Less bugs & increased productivity
- Fluent & gradual transition
- Easy initial setup
- Great documentation, good for beginners
React ❤️ TypeScript
{"metaMigratedAt":"2023-06-14T20:16:37.877Z","metaMigratedFrom":"YAML","title":"React ❤️ TypeScript","breaks":true,"slideOptions":"{\"theme\":\"dark\",\"controls\":false,\"transition\":\"fade\"}","contributors":"[{\"id\":\"97ec97f6-1343-47f7-b8b5-4f6f3c98cffe\",\"add\":9402,\"del\":2433},{\"id\":\"53c655a0-3881-4259-afc8-68b5fcfce041\",\"add\":290,\"del\":2}]"}