How to use the createExportDeclaration function from typescript

Find comprehensive JavaScript typescript.createExportDeclaration code examples handpicked from public code repositorys.

typescript.createExportDeclaration is a function that creates a new export declaration in TypeScript.

418
419
420
421
422
423
424
425
426
427
    ]
    : []),
...(needsEmptyExport
    ? [
        // Put an empty export {}; at the bottom to force TypeScript to treat the script as a module
        ts.createExportDeclaration(
            undefined,
            undefined,
            ts.createNamedExports([]),
            undefined,
fork icon116
star icon294
watch icon29

+ 3 other calls in file

43
44
45
46
47
48
49
50
51
52
}
// Add the transform operations.
const factoryClassName = entryModule.className + 'NgFactory';
const factoryModulePath = normalizedEntryModulePath + '.ngfactory';
const namedExports = ts.createNamedExports([ts.createExportSpecifier(undefined, ts.createIdentifier(factoryClassName))]);
const newImport = ts.createExportDeclaration(undefined, undefined, namedExports, ts.createLiteral(factoryModulePath));
const firstNode = ast_helpers_1.getFirstNode(sourceFile);
if (firstNode) {
    ops.push(new interfaces_1.AddNodeOperation(sourceFile, firstNode, newImport));
}
fork icon0
star icon0
watch icon1

How does typescript.createExportDeclaration work?

The typescript.createExportDeclaration function is a part of the TypeScript compiler API that allows creating a new export declaration node in the Abstract Syntax Tree (AST) for a TypeScript file. The function takes an object as an argument that describes the properties of the new export declaration node, such as the exported symbol, the source file, the module specifier, and others, and returns a new ExportDeclaration node object that can be added to the AST of a TypeScript file using the TypeScript compiler API. This can be useful when generating TypeScript code programmatically, as it allows dynamically adding new exports to a TypeScript module during runtime.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
import * as ts from "typescript";

// Create a named export declaration
const exportDeclaration = ts.createExportDeclaration(
  undefined,
  undefined,
  ts.createNamedExports([
    ts.createExportSpecifier(undefined, ts.createIdentifier("myFunction")),
  ])
);

// Print the export declaration node
console.log(exportDeclaration);

In this example, we import the typescript module and use the createExportDeclaration() function to create a named export declaration node with one export specifier that references the identifier "myFunction". The resulting node can be printed to the console for inspection or added to an AST and emitted as TypeScript code.

Other functions in typescript

Sorted by popularity

function icon

typescript.SyntaxKind is the most popular function in typescript (82777 examples)