Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

376
Views
NodeJs: read from file with type definitions and write to file as class definition

I have a typescript file with types definitions. I need to find for specific type name and write it to another file but as a class. For example:

type exampleOne = {
    atrA: string
    atrB: number
}
type exampleTwo = {
    atrA: number
    atrB: string
    atrC: string
}

and write exampleTwo to another file as:

class exampleTwo {
    atrA: number
    atrB: string
    atrC: string
}

I have this idea but I don't know how to implement it:

  1. read the file
  2. find the type name I want
  3. select from the beginning of the line where what I am looking for is, until the next closing bracket
  4. replace the word 'type' by 'class' and suppress the equal sign
  5. write to another file
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

you maybe are looking for this: https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html

in this case maybe you are trying typing something like this?

class exampleTwo {
  atrA: number;
  atrB: string;
  atrC: string;

constructor(atrA: number, atrB: string, atrC: string)
  this.atrA = atrA;
  this.atrB = atrB;
  this.atrC = atrC;
}

const newExample = new exampleTwo(2, 'hello', 'world');
about 4 years ago · Juan Pablo Isaza Report

0

Since I don't found a way to solve this, I finally solved this by making my own script in bash. I could use javascript but I prefer bash:

# Take as argument a name for reference
NEW=$1

# Verify argument is passed
if [ $# -ne 1 ]; then
    echo "One argument is required."
    exit 1
fi

# Location where the types are
PRISMA_URL='./node_modules/.prisma/client/index.d.ts'

# Location to save the file
NEW_DTO_CREATE_URL="./src/dtos/create-"${NEW}".dto.ts"

# Verify if already exists that file
if [ -f ${NEW_DTO_CREATE_URL} ]; then
    echo "Already exists"
else

  # Get the line number where starts the type I need
  LINE_N=$(grep -nm1 "export type ${NEW}CreateManyInput = {" ${PRISMA_URL} | grep -Po '^[^:]+')
  echo "export class Create${NEW^}Dto {" >> ${NEW_DTO_CREATE_URL}
  # Read all the line content on that line number
  LINE=$(head -n ${LINE_N} ${PRISMA_URL} | tail -1)

  # Start the loop. While LINE isn't a closed bracket...
  while [[ ${LINE} != *"}"* ]]; do
      ((LINE_N+=1))
      LINE=$(head -n ${LINE_N} ${PRISMA_URL} | tail -1)
      echo "${LINE}" >> ${NEW_DTO_CREATE_URL}
  done
fi
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!