You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This however makes it impossible to be used in all kinds of parsers or validators, which require a value as an input, not the type, as those are stripped during compilation.
A solution to this is ti generate a type for every corresponding enum in a form of:
Our existing `ts-morph` implementation below (click to expand)
// This script uses ts-morph to generate a "runtime enums", which are// simply an array of all possible union types.// The values are based on generated database enums and are required// in order for us to be able to use those types in all kind of validators.// It also creates additional copy of the same union type, with the same name// and based on the array readonly values for convinience.import{Project,SyntaxKind,VariableDeclarationKind}from'ts-morph'constinputFilePath=process.argv[2]constenumsFilePath=process.argv[3]if(!inputFilePath){thrownewError(`generate-runtime-enums.ts requires input path to be specified as 1st argument`)}if(!enumsFilePath){thrownewError(`generate-runtime-enums.ts requires output path to be specified as 2nd argument`)}console.log(`> Generating runtime enums from ${inputFilePath} at ${enumsFilePath}...`)constproject=newProject()constoriginalFile=project.addSourceFileAtPath(inputFilePath)constoutputFile=project.createSourceFile(enumsFilePath,'',{overwrite: true})constgeneratedEnums=originalFile.getTypeAliasOrThrow('Database').getTypeNodeOrThrow().getFirstDescendant((node)=>node.isKind(SyntaxKind.PropertySignature)&&node.getName()==='public')?.getFirstDescendant((node)=>node.isKind(SyntaxKind.PropertySignature)&&node.getName()==='Enums')?.getDescendantsOfKind(SyntaxKind.PropertySignature)if(!generatedEnums){thrownewError(`No enums found, this should never happen; Tell Kamil he messed up and should fix it.`)}for(constenumPropofgeneratedEnums){constname=enumProp.getName()constvalues=enumProp.getTypeNodeOrThrow().getType().getUnionTypes().map((type)=>type.getLiteralValue()).filter((value)=>typeofvalue==='string')outputFile.addVariableStatement({declarationKind: VariableDeclarationKind.Const,declarations: [{
name,initializer: `[${values.map((value)=>`'${value}'`).join(', ')}] as const`,},],isExported: true,})outputFile.addTypeAlias({
name,type: `(typeof ${name})[number]`,isExported: true,})}outputFile.saveSync()
The text was updated successfully, but these errors were encountered:
Currently our enums are generated like so:
This however makes it impossible to be used in all kinds of parsers or validators, which require a value as an input, not the type, as those are stripped during compilation.
A solution to this is ti generate a type for every corresponding enum in a form of:
and somehow expose it publicly.
Our existing `ts-morph` implementation below (click to expand)
The text was updated successfully, but these errors were encountered: