Why Use TypeScript?
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 2 of 73.
TypeScript offers three major benefits that make development safer and more efficient.
Catch Errors Early: TypeScript's static type checking catches errors during development, before your code runs. This prevents bugs that might only surface when users interact with your application.
Consider this common JavaScript mistake:
// JavaScript - this bug won't be caught until runtime
function calculateTotal(price, tax) {
return price + tax;
}
calculateTotal("50", 0.1); // Returns "500.1" instead of 55!With TypeScript, this error is caught immediately:
// TypeScript - error caught during development
function calculateTotal(price: number, tax: number): number {
return price + tax;
}
calculateTotal("50", 0.1); // Error: Argument of type 'string' is not assignable to parameter of type 'number'Better Code Readability: Type annotations serve as documentation, making it clear what kind of data functions expect and return. This helps both you and your teammates understand code faster.
Enhanced Development Tools: TypeScript enables superior autocompletion, refactoring, and navigation in code editors. Your editor knows exactly what properties and methods are available on each variable.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Cheat sheet
TypeScript provides three major benefits:
Catch Errors Early: Static type checking catches errors during development before code runs:
// TypeScript catches type errors immediately
function calculateTotal(price: number, tax: number): number {
return price + tax;
}
calculateTotal("50", 0.1); // Error: string not assignable to numberBetter Code Readability: Type annotations serve as documentation, making it clear what data functions expect and return.
Enhanced Development Tools: Enables superior autocompletion, refactoring, and navigation in code editors.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To TypeScript
1Getting Started with TS
What is TypeScript?Why Use TypeScript?Your First TypeScript CodeCompilation Process & ErrorsRecap: Introduction to TS4Working with Functions
Typing Params & Return ValuesTyping Arrow FunctionsThe 'void' Return TypeOptional Parameters with '?'Default Parameter ValuesTyping Rest ParametersDefining Function TypesRecap: Building Typed Funcs2Core Types
Basic Types: str, num, booleanThe 'any' Type: Escape HatchThe 'unknown' TypeWorking with 'null' & 'undef'Type Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Types: Aliases, Unions & Inter
Type Aliases for PrimitivesUnion Types ('|')Working with Union TypesLiteral TypesIntersection Types ('&')Combining Type AliasesRecap: Advanced Type Combos3Data Structure: Arrays & Tuple
Typed Arrays'readonly' Modifier for ArraysWhat is a Tuple?Declaring and Accessing TuplesDestructuring TuplesReadonly TuplesMulti-dimensional Typed Arrays Spread Operator with ArraysRecap: Arrays and Tuples