Tengo un archivo mecanografiado con definiciones de tipos. Necesito encontrar un nombre de tipo específico y escribirlo en otro archivo pero como una clase. Por ejemplo:
type exampleOne = { atrA: string atrB: number } type exampleTwo = { atrA: number atrB: string atrC: string }y escriba exampleTwo en otro archivo como:
class exampleTwo { atrA: number atrB: string atrC: string }Tengo esta idea pero no se como implementarla:
tal vez esté buscando esto: https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html
en este caso, ¿quizás estés intentando escribir algo como esto?
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');Como no encontré una manera de resolver esto, finalmente lo resolví creando mi propio script en bash. Podría usar javascript pero prefiero 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