How to use the createClassDeclaration function from typescript

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

typescript.createClassDeclaration is a method in the TypeScript compiler API that creates a new class declaration node.

307
308
309
310
311
312
313
314
315
316
}
function createWrappedClass(hostNode, statements) {
    const name = hostNode.name.text;
    const updatedStatements = [...statements];
    if (ts.isClassDeclaration(hostNode)) {
        updatedStatements[0] = ts.createClassDeclaration(hostNode.decorators, undefined, hostNode.name, hostNode.typeParameters, hostNode.heritageClauses, hostNode.members);
    }
    const pureIife = ast_utils_1.addPureComment(ts.createImmediatelyInvokedArrowFunction([
        ...updatedStatements,
        ts.createReturn(ts.createIdentifier(name)),
fork icon0
star icon0
watch icon1

How does typescript.createClassDeclaration work?

typescript.createClassDeclaration is a function in the TypeScript compiler API that creates a new ClassDeclaration node that can be added to a TypeScript AST, representing a class declaration with properties, methods, and an optional constructor. The function takes a variety of arguments, such as the class name, modifiers, heritage clauses, and class body, and returns a new ClassDeclaration node.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const ts = require("typescript");

// Create a class declaration node
const classNode = ts.createClassDeclaration(
  undefined, // decorators
  [
    // modifiers
    ts.createModifier(ts.SyntaxKind.ExportKeyword),
  ],
  "MyClass", // name
  [
    // type parameters
    ts.createTypeParameterDeclaration("T"),
  ],
  [
    // heritage clauses
    ts.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [
      ts.createExpressionWithTypeArguments(
        [],
        ts.createIdentifier("MyBaseClass")
      ),
    ]),
  ],
  [
    // members
    ts.createMethod(
      undefined, // decorators
      [
        // modifiers
        ts.createModifier(ts.SyntaxKind.PublicKeyword),
      ],
      undefined, // asterisk token
      "myMethod", // name
      undefined, // question token
      undefined, // type parameters
      [
        // parameters
        ts.createParameter(
          undefined, // decorators
          undefined, // modifiers
          undefined, // dot dot dot token
          "arg1", // name
          undefined, // question token
          ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword), // type
          undefined // initializer
        ),
      ],
      ts.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword), // return type
      ts.createBlock([], true) // body
    ),
  ]
);

// Print the class declaration node as TypeScript code
console.log(
  ts
    .createPrinter()
    .printNode(
      ts.EmitHint.Unspecified,
      classNode,
      ts.createSourceFile("", "", ts.ScriptTarget.Latest)
    )
);

This code will output the following TypeScript code: typescript Copy code

Other functions in typescript

Sorted by popularity

function icon

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