Code Monkey home page Code Monkey logo

advanced-typescript-concepts's Introduction

This repo contains the advanced concepts of Typescript which could come in handy/used in big codebases.

  1. Exclude : Helps exclude one or more properties from interface/type, eliminating the need to create a new type/interface.

  2. Pick : Helps pick desired properties from a interface/type, allowing us to reuse the same type/interface again and again.

  3. Records : Make key-value pair assignments concise and easy. We can use interfaces/types for that purpose as well, but records provide concise and clean syntax. Provides a way to create a type with a fixed set of properties. Similar to defining an object with fixed keys using an interface or type, but specifically useful for mapping known keys to specific value types.

type UserRoles = "admin" | "user" | "guest";
type RolePermissions = Record<UserRoles, boolean>;

const permissions: RolePermissions = {
  admin: true,
  user: false,
  guest: false,
};
  1. Maps : JS concept, helps create maps in TS much similar to C++ maps by using key-value pair concept. A JavaScript object that allows for more flexible key types and ordered entries. Useful for collections where you need keys that are not strings or when you need to maintain insertion order.
const map = new Map<string, number>();
map.set("one", 1);
map.set("two", 2);
  1. Zod Type Inference : Helps re-use the zod schema as a type in TS in frontends and backends both, eliminating the need to re-create a new type/interface based on the same schema.

  2. Interfaces : Can be extended (merged) and implemented by classes. Better for defining object shapes and ensuring certain structure in class implementations.

  3. Types : More versatile, can create unions and intersections, and can alias primitive types and other complex types. Cannot be re-opened (extended) like interfaces.

interface User {
  name: string;
  age: number;
}

const user: User = {
  name: "Alice",
  age: 25,
};

advanced-typescript-concepts's People

Contributors

rajneesh069 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.