Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀 [Feature Request] - Conversion of Javascript code to Typescript code in server & app modules #22

Closed
prabh1234assla opened this issue May 15, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request Future issue needs time gssoc issues for the GSSOC level3 comparatively harder to solve

Comments

@prabh1234assla
Copy link

prabh1234assla commented May 15, 2024

Why Ts and why not pure vanilla js?

TypeScript stands out from vanilla JavaScript with its outstanding features like static typing, interfaces, enums, generics, union types, type guards, and decorators. These features collectively enhance code quality, readability, and maintainability, while also significantly reducing the occurrence of bugs and errors. By providing a more structured and expressive way to write code, TypeScript empowers developers to build complex applications with confidence, offering a smoother development experience and facilitating collaboration within teams.

Why I am the right fit for this project
I've successfully utilized TypeScript across projects integrating React-Three/Fiber, React-Three/drei, Three.js and React Spring into Next.js for immersive 3D websites, implemented WebRTC and sockets in TypeScript codebases, and built scalable solutions with Gatsby, GraphQL, and TypeScript, all with a focus on robustness and efficiency. I think I have required know-how on how to handle the TypeScript codebases.

Here is how I will handle my js to ts conversion

  1. Install TypeScript: I will begin by installing TypeScript globally using npm or yarn.
    npm install -g typescript

  2. Rename Files: Change the file extensions of all JavaScript files to TypeScript files. For JSX files, I will change the extension to .tsx.

  3. Enable TypeScript Configuration: If project doesn't have a tsconfig.json file, I will create one using the following command:
    tsc --init

  4. Start Converting: Begin converting my JavaScript code to TypeScript by following these steps:

  • Static Typing: Add type annotations for variables, function parameters, and return types to enhance code reliability.
let firstName: string = 'John';
let age: number = 30;

function add(a: number, b: number): number {
  return a + b;
}
  • Interfaces: Define interfaces to describe the structure of objects, promoting code clarity and maintainability.
interface Person {
  firstName: string;
  lastName: string;
  age: number;
}

let person: Person = {
  firstName: 'John',
  lastName: 'Doe',
  age: 30,
};
  • Clean Class Design: When converting existing JavaScript classes to TypeScript, refactor them to adhere to clean and object-oriented design principles, ensuring the new TypeScript codebase is more organized and maintainable.

  • Enums: Replace constants with enums to improve code readability and maintainability.

enum Color {
  Red,
  Green,
  Blue,
}

let c: Color = Color.Green;
console.log(c); // Output: 1

  • Generics: Utilize generics for writing reusable code that can work with a variety of types, enhancing code flexibility and scalability.
function identity<T>(arg: T): T {
  return arg;
}

let output = identity<string>('hello');
console.log(output); // Output: hello
  • Union Types: Specify that a variable can have one of several possible types, enhancing code flexibility.
let value: string | number;
value = 'hello';
console.log(value); // Output: hello
value = 42;
console.log(value); // Output: 42
  • Type Guards: Narrow down the type of a variable within a block of code, ensuring type-safe operations.
function printName(name: string | string[]): void {
  if (typeof name === 'string') {
    console.log(name.toUpperCase());
  } else {
    console.log(name.join(', '));
  }
}

printName('John'); // Output: JOHN
printName(['John', 'Doe']); // Output: John, Doe
  • Type Inference: Allow TypeScript to infer the type of a variable based on its value, reducing the need for explicit type annotations.
let x = 3; // TypeScript infers that x is of type number
console.log(x); // Output: 3
  • Optional Chaining: Safely access nested properties without worrying about null or undefined values, preventing runtime errors.
interface Person {
  firstName: string;
  lastName?: string;
}

let person: Person = {
  firstName: 'John',
};

console.log(person.lastName?.toUpperCase()); // Output: undefined
  • Decorators: Add metadata to classes and their members to enhance code functionality without modifying the original code, commonly used in frameworks like Angular, but if needed I can add this to the current project.
@sealed
class BugReport {
  type = "report";
  title: string;
 
  constructor(t: string) {
    this.title = t;
  }
}

function sealed(constructor: Function) {
  Object.seal(constructor);
  Object.seal(constructor.prototype);
}

When @Sealed is executed, it will seal both the constructor and its prototype, and will therefore prevent any further functionality from being added to or removed from this class during runtime by accessing BugReport.prototype or by defining properties on BugReport itself.

Additional things I will consider While converting the Js codebase to Ts codebase :

  1. Resolve Compilation Errors:
    As I convert your code, TypeScript might raise compilation errors due to type inconsistencies or other issues. I will make sure to address these errors one by one by refining your type annotations and ensuring compatibility with TypeScript's type system.

  2. Incremental Adoption:
    If the codebase is extensive, I will consider adopting TypeScript incrementally. I will start by converting individual modules or components and gradually expand the TypeScript coverage across the project.

  3. Testing and Validation:
    Test the TypeScript code thoroughly to ensure it behaves as expected. Use TypeScript's type system to catch errors early in the development process, reducing the likelihood of runtime issues.

  4. Continuous Integration:
    Integrate TypeScript conversion into continuous integration pipeline to ensure that new changes adhere to TypeScript standards and do not introduce regressions.

  5. Documentation and Training:
    Update project documentation to familiarize team members with TypeScript features.

@prabh1234assla prabh1234assla added the enhancement New feature or request label May 15, 2024
@Girishbari
Copy link
Owner

Hey, @prabh1234assla, it's really fascinating that you know ts very well, converting js to ts is in the list of doings, but before that I was thinking to make this repo a monorepo, this will separate both our application and can provide more structured view.
There is already a issue made for this, if you're comfortable you can take it..

@Girishbari Girishbari added level2 comparatively medium to solve Future issue needs time gssoc issues for the GSSOC labels May 15, 2024
@prabh1234assla
Copy link
Author

okay @Girishbari i am fascinated to see your response and yes I am comfortable in doing so. Can u assign me this MONOREPO task. I will be amazed to take and work on it.

prabh1234assla added a commit to prabh1234assla/comic-cult-GSSOC that referenced this issue May 21, 2024
- CatchAsync module has been added with MY PERSONAL IMPLEMENTATION OF CATCHASYNC
- SignToken module isn't known so added a Dummy Implementation
- TsNode added for starting server.ts

Fix: Girishbari#22
prabh1234assla added a commit to prabh1234assla/comic-cult-GSSOC that referenced this issue May 22, 2024
- CatchAsync module has been added with MY PERSONAL IMPLEMENTATION OF CATCHASYNC
- SignToken module isn't known so added a Dummy Implementation
- TsNode added for starting server.ts

Fix: Girishbari#22
@Girishbari Girishbari added level3 comparatively harder to solve and removed level2 comparatively medium to solve labels May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Future issue needs time gssoc issues for the GSSOC level3 comparatively harder to solve
Projects
None yet
Development

No branches or pull requests

2 participants