Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

378
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda