TypeScript
TypeScript
md 2025-05-13
TypeScript
Installing TypeScript
1. Install Node.js
node -v
npm -v
Run:
Verify with:
tsc -v
Create app.ts:
Run:
tsc app.ts
Run with:
node app.js
1/6
TypeScript.md 2025-05-13
tsc --watch
Generate tsconfig.json:
tsc --init
Troubleshooting
Update TypeScript:
Functions
1. Function Declaration: Functions are defined with type annotations for parameters and return types.
2. Arrow Functions: Shorter syntax for writing functions, with lexical this.
3. Function Types: You can define a function signature type using a type alias.
Interfaces
1. Basic Interface Definition: Defines the shape of an object with specific properties and their types.
2. Optional Properties: Use ? to make properties optional.
3. Read-only Properties: Use readonly to prevent modification of properties.
4. Methods in Interfaces: Define method signatures in the interface to specify required methods for objects.
5. Extending Interfaces: Use extends to create new interfaces that build on existing ones.
6. Function Types: Use interfaces to define function signatures.
7. Interface as a Contract: Classes or objects must implement the structure defined by the interface.
8. Interface vs Type Alias: Interfaces are mainly used for objects, while type aliases are more flexible for other complex types.
2/6
TypeScript.md 2025-05-13
Duck Typing
Feature Explanation
Key Principle “If it walks like a duck and quacks like a duck…”
Useful For Flexibility and code reuse with functions and interfaces
Function Types
Feature Example Syntax Description
Type Alias for Function type Fn = (a: number) => string; Defines a reusable function signature
Inline Type let fn: (x: string) => number Directly declares function type
Function as Parameter (fn: (a: number, b: number) => number) Accepts a typed function as argument
Interface Function interface Fn { (x: string): boolean } Function shape using an interface
- Feature Description
splitting across files Use /// to merge multiple files into one namespace
1. Basic Namespace:
3/6
TypeScript.md 2025-05-13
namespace Utils {
export function greet(name: string) {
return `Hello, ${name}`;
}
}
2. Nested Namespaces:
namespace Game {
export namespace Engine {
export function start() {
console.log("Game started");
}
}
}
Game.Engine.start();
import Imports code from other modules to use it in the current file.
4/6
TypeScript.md 2025-05-13
Export Types
Import:
Import:
import { define(['module'],
const { add } =
Import/Export add } from function(module) { System.import('module').then(...)
require('module');
'module'; ... });
Browsers,
Node.js (Older Mainly in Browsers
Supported By Node.js All formats, used in various projects
versions) (via RequireJS)
(v12+)
Module Vs Namespace
Feature Module Namespace
Declaration
Uses export and import keywords Uses namespace and export within the namespace
Syntax
Code Typically confined to a single file (but can span multiple files in
Can be split across multiple files
Separation certain scenarios)
Each module is its own scope (i.e., Everything inside a namespace shares the same global scope
Scope
private by default) unless explicitly exported
5/6
TypeScript.md 2025-05-13
Bundling Supported (webpack, rollup, etc.) No support for bundling, everything is part of the global scope
File Usage Each file is treated as a module Can span multiple files but typically in a single global space
You are working in modern TypeScript or JavaScript environments that support ES6 modules.
You want to take advantage of tree-shaking, which removes unused exports in the build process.
You want to separate your code into different files for better code organization.
You are using bundlers like Webpack, Rollup, or Parcel.
You need asynchronous loading or lazy loading of your code.
You are working with legacy code or in an environment where ES6 modules are not available.
You are building small-scale applications and don't need modular code splitting.
You don't require tree-shaking or dynamic imports.
Your project is small enough to benefit from grouping related functions together in a single space.
Generics
Concept Description
Generics Write reusable components that work with any data type while maintaining type safety.
Type Parameters Placeholders (e.g., <T>) for types that are provided when the function or class is used.
Generic Functions Functions that work with various types without compromising on type safety.
Generic Constraints Restrict the types a generic can be to those that satisfy a specific condition (using extends).
Connect
GitHub omteja04
LinkedIn omteja
6/6