JavaScript in NOT Statically Typed, JS is Dynamically Typed Language
TypeScript is a free and open-source high-level programming language developed by Microsoft
that adds static typing with optional type annotations to JavaScript.
It is designed for the development of
large applications and transpiles to JavaScrip
*** Statically Typed Languages
In statically typed programming languages,
type checking occurs at compile time.
*** Dynamically Typed Languages
Conversely, in dynamically typed languages,
type checking takes place at runtime or execution time.
✅ Advantages of Dynamic Typing in JavaScript
Variable Types Are Not Declared
Flexible Variable Assignments
Run-Time Type Checking
Prototypal Inheritance
❌ Disadvantages of Dynamic Typing in JavaScript
Runtime Errors
No Compile-time Type Checking
Difficulty in Refactoring
Less Suitable for Large-scale Applications
The most significant impact of TypeScript
Reducing Runtime Errors
Other Befefits of Typescristusing in JS
1.Static Typing, Early Detection of Type-related Errors
2.Type Inference
3.Better IDE Support
4.Static Null Checking
5.Interoperability
JS + TypeScript Type-Safe Code
Let’s examine the basic concepts and examples of how we can use them.
var
Array
Tuple
Enum (Enumeration) in TypeScript
TypeScript does not create a new object in memory for each instance.
Instead, an enum
Any - same as turn OFF - TypeScript
❌ - not recommended to use
Function in TypeScript
We need to specify the return type of the function.
We also need to define the types of parameters it takes.
undefined, null
never in TypeScript
type of values that never occur.
Object in TypeScript
”object” usless to use as a type in TypeScript.
better type checking and code descriptive approach and shape of the object.
Okay, now let’s dive into some more interesting tips and advanced examples of TypeScript usage:
Type assertions
Best Practice: Type signature
Clarity and Documentation:
Compile-Time Type Checking
Maintainability
Refactoring Safety:
Generics and Advanced Typing
Rest JS + Tuple
Good prictice to set up the Limit
when you want to ensure that the function is not called with a meaningful “this” context.
Best Practice: Generics in TypeScript
provide flexibility with multiple types.
User-Defined Type Guards “instanceof”
Generics with two different type variables, T and U.