What is TypeScript?

TypeScript is a programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript, which means every valid JavaScript code is also valid TypeScript code, but TypeScript adds additional features.

Illustration of What is TypeScript?
Illustration of What is TypeScript?

💡 Core Idea

TypeScript enhances JavaScript by adding static types, enabling developers to catch errors during development rather than at runtime.

In plain terms, TypeScript helps you write safer and more maintainable code by introducing a type system and other modern programming features.

JavaScript vs TypeScript
AspectJavaScriptTypeScript
TypingDynamically typedStatically typed (optional)
CompilationInterpreted by browsersCompiled to JavaScript before running
ToolingBasic error detectionAdvanced error detection & IDE support
Learning CurveLowerRequires learning types & compilation
Use CasesWeb apps, scriptsLarge-scale apps, enterprise projects

TypeScript files use the .ts extension and need to be transpiled (converted) to plain JavaScript before they can run in browsers or JavaScript environments.

💡 Why Use TypeScript?

  • Catch common errors early through static type checking.
  • Improve code readability and maintainability by explicitly defining data shapes.
  • Enjoy better tooling support like autocompletion, refactoring, and navigation.
  • Easier collaboration in large teams and complex projects.

📌 Deep Dive: Simple TypeScript Example

TYPESCRIPT
function greet(name: string) {
  return "Hello, " + name.toUpperCase();
}

// This will cause a compile-time error
// greet(42); 

console.log(greet("Alice"));
Output
Hello, ALICE

In this example, name must be a string. If you try to call greet with a different type like a number, TypeScript will flag an error before running the code.

⚠️ Important

TypeScript only checks types during development and compilation. Once compiled, the output is plain JavaScript, which does not enforce types at runtime.

In summary, TypeScript is a powerful tool that builds on JavaScript by adding static typing and other features to improve developer experience and code quality.