ReasonJun

Typescript : Concept 본문

Frontend/Typescript

Typescript : Concept

ReasonJun 2023. 6. 9. 11:34
728x90

TypeScript is an open-source programming language developed by Microsoft. It is a superset of JavaScript, which means that any valid JavaScript code is also valid TypeScript code. TypeScript introduces optional static typing and provides additional features and capabilities that enhance the development experience and help catch errors at compile-time.

 

Here are some key features and concepts of TypeScript:

  1. Static Typing: TypeScript introduces static typing, allowing developers to specify types for variables, function parameters, return values, and more. This enables catching type-related errors during development, provides better tooling support (such as autocompletion and type inference), and enhances code readability and maintainability.
  2. Type Annotations: TypeScript uses type annotations to define the types of variables, function parameters, and return values. Type annotations are expressed using a syntax that resembles JavaScript, but with additional type information. For example:
let age: number = 25;
function greet(name: string): string {
  return `Hello, ${name}!`;
}
  1. Type Inference: TypeScript has a powerful type inference system that automatically infers types based on the context. This means that in many cases, you don't have to explicitly specify types, as TypeScript can deduce them from the assigned values. For example:
let age = 25; // TypeScript infers the type as number
let name = 'John'; // TypeScript infers the type as string
  1. Interfaces: TypeScript supports interfaces, which allow you to define the shape of objects. Interfaces specify the names and types of properties that an object should have. Interfaces can be used to enforce contracts and provide type checking for objects. For example:
interface Person {
  name: string;
  age: number;
}

function greet(person: Person): string {
  return `Hello, ${person.name}! You are ${person.age} years old.`;
}
  1. Classes: TypeScript supports class-based object-oriented programming concepts, including classes, inheritance, access modifiers (public, private, protected), and more. Classes provide a way to define blueprints for objects and encapsulate data and behavior. For example:
class Animal {
  private name: string;

  constructor(name: string) {
    this.name = name;
  }

  move(distance: number) {
    console.log(`${this.name} moves ${distance} meters.`);
  }
}

class Dog extends Animal {
  bark() {
    console.log('Woof!');
  }
}

const dog = new Dog('Buddy');
dog.move(10); // Output: Buddy moves 10 meters.
dog.bark(); // Output: Woof!
  1. Generics: TypeScript supports generics, which enable writing reusable and type-safe functions and data structures. Generics allow you to define placeholders for types that are specified when the code is used. This provides flexibility and helps ensure type correctness. For example:
function identity<T>(value: T): T {
  return value;
}

let result = identity<number>(10); // result is of type number
  1. Module System: TypeScript has built-in support for modules, allowing you to organize your code into separate files and specify dependencies between them. Modules can be imported and exported using import and export statements, facilitating code sharing and encapsulation.

TypeScript compiles down to plain JavaScript, which means that it can run on any JavaScript runtime and can be integrated into existing JavaScript projects. The TypeScript compiler (tsc) transpiles TypeScript code into JavaScript code, providing type checking and generating corresponding JavaScript files that can be executed in browsers or on server-side platforms.

 

Overall, TypeScript offers the advantages of static typing and additional language features to help catch errors early, improve code quality, and enhance the development experience for large-scale JavaScript projects.

728x90
Comments