Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

377
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda