I want to generate a class whose members has more than one 'class-validator' Decorator to check the type of property. I think i can use ts.factory.createDecorator to do that, but how can i get a ts.Expression?
found this, check maybe will be helpfull for you
/* output
@niceDecorator(“bar”)
class foo {
}
*/
const p = ts.createIdentifier(‘niceDecorator’);
const call = ts.createCall(p, undefined, [str]);
const dec = ts.createDecorator(call);
const classDec = ts.createClassDeclaration([dec], undefined, ‘foo’, undefined, undefined, undefined);
I'm trying to do the same, couldn't find how to make it.
as for code example (which is not working):
const newMembers = node.members.map(propertyNode => {
if(ts.isPropertyDeclaration(propertyNode)) {
const newDecorators = [...propertyNode.decorators];
if (propertyNode.type.kind === 149) {
// STRING ts.SyntaxKind[kind]
const newExpression = ts.factory.createDecorator(
{
_expressionBrand: 'IsString',
}
)
newDecorators.push({
kind: ts.SyntaxKind.Decorator,
parent: propertyNode,
expression: newExpression,
});
}
const updRes = ts.factory.updatePropertyDeclaration(
propertyNode,
newDecorators,
propertyNode.modifiers,
propertyNode.name,
undefined,
propertyNode.type,
propertyNode.initializer
)
return updRes;
}
const updClassRes = ts.factory.updateClassDeclaration(
node,
node.decorators,
node.modifiers,
node.name,
node.typeParameters,
node.heritageClauses,
newMembers
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.