I want to create type files like this programatically:
export type Car = {
color: string;
// ... tons of properties
};
Thats seems pretty easy using ts-morph addTypeAlias method:
sourceFile.addTypeAlias({
name: 'Car',
type: theType,
isExported: true,
});
Though unfortunately theType here must be string | WriterFunction.
And I want it to be a TypeLiteral! Is there a way to this with ts-morph that I am missing? Ofcourse I could construct my type literal as a string by some looping and string concatenation etc, but I would really like to avoid building my files that way - then I might as well use template/stub files and fs.
Any other way to do this in a more programmatic/Node-class based approach?
Introduction
Let's consider the following Node.js version as the current version:
Let's consider the following npm version as the current version:
Let's consider the following versions of the dependencies as the current versions (an excerpt from the
package.jsonfile):Overview
Currently, it seems there is no straightforward solution.
Please, see the following seemingly related GitHub issues:
Let's consider some possible solutions.
Possible solution:
ts-morph: Use object type writer (Writers.objectType()static method)It may be considered as more programmatic, but it is still a writer-based (not a node-based) approach.
References:
Writers.objectType()static method. ts-morph/Writers.ts at 17.0.1 · dsherret/ts-morph.Draft example
src/index-ts-morph.tsfileRunning script and checking its output
Please, note the strange indentation.
Probably, it is caused by a
ts-morphdefect?Possible solution:
typescript: Usetypescriptinstead ofts-morphReferences:
Draft example
src/index-typescript.tsfileRunning script and checking its output