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:
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');
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