Skip to content

hadiulofficial/typescript-intro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Typescript

TypeScript is a statically typed, object-oriented, and multi-paradigm language that builds on JavaScript by adding optional type annotations and class-based object-oriented programming. It was developed and maintained by Microsoft and has been gaining popularity among developers for its ability to catch errors and provide better maintainability for large-scale projects. Here are some of the key features of TypeScript with examples:

  1. Types: TypeScript adds optional type annotations to JavaScript, making it easier to catch errors during development. For example, you can declare a variable with a specific type, such as:
let name: string = "Hadiul Islam";
  1. Interfaces: Interfaces are a way to define a contract for objects to follow in TypeScript. For example, you can define an interface for a person as follows:
interface Person {
  name: string;
  age: number;
}
  1. Classes: TypeScript introduces the concept of classes, which allows for object-oriented programming in JavaScript. For example, you can define a class for a person as follows:
class Person {
  name: string;
  age: number;
  
  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }
}
  1. Generics: Generics allow you to write reusable code that can work with any type. For example, you can define a generic function that takes an array of any type and returns the same array in reverse:
function reverseArray<T>(array: T[]): T[] {
  return array.reverse();
}
  1. Decorators: Decorators are a way to add additional behavior to classes, methods, and properties. For example, you can use the @log decorator to log a message whenever a method is called:
function log(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
  const originalMethod = descriptor.value;

  descriptor.value = function(...args: any[]) {
    console.log(`${propertyKey} method was called`);
    return originalMethod.apply(this, args);
  };

  return descriptor;
}

class Calculator {
  @log
  add(a: number, b: number): number {
    return a + b;
  }
}
  1. Namespaces: Namespaces allow you to organize your code into logical groups and avoid naming collisions. For example, you can define a namespace for your utility functions:
namespace Utils {
  export function logMessage(message: string) {
    console.log(message);
  }
}

These are just a few of the features of TypeScript, and there are many more that make it a powerful tool for building large-scale and maintainable applications.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published