How to use the createNamespaceImport function from typescript

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

The typescript.createNamespaceImport function creates a NamespaceImport node representing a namespace import declaration in a TypeScript AST.

13
14
15
16
17
18
19
20
21
22
function insertStarImport(sourceFile, identifier, modulePath, target, before = false) {
    const ops = [];
    const allImports = ast_helpers_1.collectDeepNodes(sourceFile, ts.SyntaxKind.ImportDeclaration);
    // We don't need to verify if the symbol is already imported, star imports should be unique.
    // Create the new import node.
    const namespaceImport = ts.createNamespaceImport(identifier);
    const importClause = ts.createImportClause(undefined, namespaceImport);
    const newImport = ts.createImportDeclaration(undefined, undefined, importClause, ts.createLiteral(modulePath));
    if (target) {
        ops.push(new interfaces_1.AddNodeOperation(sourceFile, target, before ? newImport : undefined, before ? undefined : newImport));
fork icon0
star icon0
watch icon1

40
41
42
43
44
45
46
47
48
49
if (!(relativePath.startsWith('./') || relativePath.startsWith('../'))) {
    // 'a/b/c' is a relative path for Node but an absolute path for TS, so we must convert it.
    relativePath = `./${relativePath}`;
}
// Create the new namespace import node.
const namespaceImport = ts.createNamespaceImport(ts.createIdentifier(`__lazy_${index}__`));
const importClause = ts.createImportClause(undefined, namespaceImport);
const newImport = ts.createImportDeclaration(undefined, undefined, importClause, ts.createLiteral(relativePath));
const firstNode = ast_helpers_1.getFirstNode(sourceFile);
if (firstNode) {
fork icon0
star icon0
watch icon1

How does typescript.createNamespaceImport work?

The typescript.createNamespaceImport function creates a NamespaceImport node, which is used to represent a namespace import declaration in a TypeScript AST. The function takes two arguments: name and alias. name is a string that specifies the name of the namespace being imported, and alias is an Identifier node that specifies the name of the local alias that will be used to reference the namespace.

When this function is called, it creates a NamespaceImport node with the given alias and name, and returns it as the result. This node can then be used as a part of a larger TypeScript AST to represent a namespace import declaration.

Ai Example

1
2
3
4
5
import * as ts from "typescript";

const namespaceImport = ts.createNamespaceImport(
  ts.createIdentifier("myNamespace")
);

This creates a namespace import for an identifier named myNamespace. The resulting namespaceImport variable can be used as an import specifier for a TypeScript import declaration.

Other functions in typescript

Sorted by popularity

function icon

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