Interesting Facts About TypeScript Basics (original) (raw)
Last Updated : 20 Mar, 2025
TypeScript is a statically typed superset of JavaScript that brings powerful tools and features to modern web development. While it may seem like just an extension of JavaScript, TypeScript introduces concepts that significantly improve code quality and maintainability.
1. TypeScript is a Superset of JavaScript
TypeScript extends JavaScript by adding static types, so all valid JavaScript code works in TypeScript. This allows easy adoption in existing projects.
JavaScript `
let message = "Hello, TypeScript!"; console.log(message);
`
2. Strong Static Typing Helps Catch Errors Early
TypeScript’s static typing ensures errors are caught at compile time, making the code more reliable and easier to debug.
JavaScript `
let age: number = 25; // age = "twenty-five"; // Error: Type 'string' is not assignable to type 'number'
`
3. Fully Compatible with Modern JavaScript
TypeScript supports modern JavaScript features (like ES6 and beyond) and transpiles them to older versions for compatibility across browsers.
JavaScript ``
const greet = (name: string): string => Hello, ${name}!
;
console.log(greet("Alice")); // Hello, Alice!
``
4. Gradual Adoption is Possible
You don’t need to rewrite your entire project. You can start with JavaScript and add TypeScript features gradually as needed.
JavaScript `
// Start with plain JavaScript let message = "Hello, TypeScript!";
// Gradually add types let message: string = "Hello, TypeScript!";
`
5. Type Inference Saves Time
TypeScript can automatically infer variable types, reducing the need to explicitly define them every time.
JavaScript ``
let name = "Alice"; // TypeScript infers name
as a string
// name = 42; // Error: Type 'number' is not assignable to type 'string'
``
6. Template Literal Types for Dynamic Strings
You can create precise and dynamic string types using template literals, making your types more flexible.
JavaScript ``
type EventName = ${T}Changed
;
type ClickEvent = EventName<"click">; // "clickChanged"
``
7. The unknown Type for Safer Code
The unknown type is safer than any because it forces type checks before using the value, preventing unintended errors.
JavaScript `
let data: unknown = "Hello"; if (typeof data === "string") { console.log(data.toUpperCase()); // Safe to use }
`
8. The never Type for Unreachable Code
The never type is used for functions that never return, such as those that throw errors or run infinite loops.
JavaScript `
function throwError(message: string): never { throw new Error(message); }
`
9. Conditional Types for Flexible Logic
You can define types based on conditions, similar to how ternary operators work in JavaScript, giving your code more flexibility.
JavaScript `
type IsString = T extends string ? true : false; type A = IsString<"hello">; // true type B = IsString<42>; // false
`
10. TypeScript Improves Code Quality
By adding static types, TypeScript makes your code more predictable, maintainable, and easier to debug.
JavaScript `
let age: number = 25; // age = "twenty-five"; // Error: Type 'string' is not assignable to type 'number'
`