ts-morph
ts-morph copied to clipboard
[Question] How to create a TypeLiteral ?
Hello,
I'm trying to create a TypeAliasDeclaration with a TypeLiteral for the type attribute but it receives only a string or a function as value.
This an example of what I want to create
export type Options = {
port?: number;
host?: string;
};
This is what I have currently
sourceFile.addTypeAlias({
type: '{ port?: number; host?: number; }',
name: 'Options',
isExported: true,
});
Is there another way to achieve that ?

By doing something like this
sourceFile.addTypeAlias({
type: new TypeLiteral({
propertySignatures: [
new PropertySignature({ hasQuestionToken: true, type: SyntaxKind.NumberKeyword, name: 'port' }),
new PropertySignature({ hasQuestionToken: true, type: SyntaxKind.StringKeyword, name: 'host' }),
]
}),
name: 'Options',
isExported: true,
});
Thanks.
Did you get an answer? I have a similar requirement.