How to use the isTSInterfaceDeclaration function from @babel/types

Find comprehensive JavaScript @babel/types.isTSInterfaceDeclaration code examples handpicked from public code repositorys.

@babel/types.isTSInterfaceDeclaration is a function in the @babel/types library for JavaScript that determines whether a given AST node represents a TypeScript interface declaration.

45
46
47
48
49
50
51
52
53
54
  }
} else if (t.isTypeAlias(node) || t.isTSTypeAliasDeclaration(node)) {
  comment.kind = 'typedef';
} else if (
  t.isInterfaceDeclaration(node) ||
  t.isTSInterfaceDeclaration(node)
) {
  comment.kind = 'interface';
} else if (t.isVariableDeclaration(node)) {
  if (node.kind === 'const') {
fork icon509
star icon0
watch icon0

+ 20 other calls in file

23
24
25
26
27
28
29
30
31
32
programPath.node.body.forEach((node) => {
  if (t.isImportDeclaration(node)) {
    ImportDeclarations.push(node)
  }

  if (!TSInterfaceDeclaration && t.isTSInterfaceDeclaration(node)) {
    TSInterfaceDeclaration = node
  }

  if (!ExportDefaultDeclaration && t.isExportDefaultDeclaration(node)) {
fork icon42
star icon91
watch icon8

How does @babel/types.isTSInterfaceDeclaration work?

When you use @babel/types.isTSInterfaceDeclaration in your JavaScript code, you are using a function that determines whether a given AST node represents a TypeScript interface declaration. To use @babel/types.isTSInterfaceDeclaration, you pass an AST node as an argument, and it returns a boolean value that indicates whether the node is a TypeScript interface declaration. An AST node is a representation of a JavaScript code structure, created by a parser. Here is an example of using @babel/types.isTSInterfaceDeclaration to check if a node represents a TypeScript interface declaration: javascript Copy code {{{{{{{ const t = require('@babel/types'); const node = /* an AST node representing a TypeScript interface declaration */; if (t.isTSInterfaceDeclaration(node)) { console.log('This is a TypeScript interface declaration'); } else { console.log('This is not a TypeScript interface declaration'); } In this example, we are using @babel/types.isTSInterfaceDeclaration to check whether an AST node represents a TypeScript interface declaration. We pass the node as an argument to the function, and then use an if statement to check whether the function returns true. If it does, we log a message to the console indicating that the node is a TypeScript interface declaration. Overall, @babel/types.isTSInterfaceDeclaration is a useful utility function that can be used in a wide range of JavaScript applications that work with AST nodes, such as code analysis tools and compilers.

189
190
191
192
193
194
195
196
197
198
  t.isClassDeclaration(declaration) ||
  t.isFunctionDeclaration(declaration) ||
  t.isTypeAlias(declaration) ||
  t.isInterfaceDeclaration(declaration) ||
  t.isTSTypeAliasDeclaration(declaration) ||
  t.isTSInterfaceDeclaration(declaration) ||
  t.isTSEnumDeclaration(declaration) ||
  t.isTSDeclareFunction(declaration) ||
  t.isTSDeclareMethod(declaration)
) {
fork icon6
star icon34
watch icon0

+ 3 other calls in file

88
89
90
91
92
93
94
95
96
97
babel.traverse(ast, {
    enter: function (path) {
        var _a;
        // alias or interface (e.g. type Props = { } or interface Props {} )
        if (types.isTSTypeAliasDeclaration(path.node) ||
            types.isTSInterfaceDeclaration(path.node)) {
            json.types = __spreadArray(__spreadArray([], ((_a = json.types) !== null && _a !== void 0 ? _a : []), true), [(0, generator_1.default)(path.node).code], false);
            path.skip();
        }
        else if (types.isTSTypeAnnotation(path.node)) {
fork icon2
star icon4
watch icon0

+ 7 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const t = require("@babel/types");
const { parse } = require("@babel/parser");

const code = `
interface Person {
name: string;
age: number;
}
`;

const ast = parse(code);
const node = ast.program.body[0];

if (t.isTSInterfaceDeclaration(node)) {
  console.log("This is a TypeScript interface declaration");
} else {
  console.log("This is not a TypeScript interface declaration");
}

In this example, we are using @babel/types.isTSInterfaceDeclaration to check whether an AST node represents a TypeScript interface declaration. We first define a string code that contains a TypeScript interface declaration for a Person object. We then parse the code into an AST using @babel/parser, and access the first node of the program body, which should represent the Person interface declaration. We then use @babel/types.isTSInterfaceDeclaration to check whether the node is a TypeScript interface declaration. We pass the node as an argument to the function, and use an if statement to log a message to the console depending on the result of the function. Note that @babel/types.isTSInterfaceDeclaration is just one of many utility functions available in the @babel/types library for working with AST nodes in JavaScript.

Other functions in @babel/types

Sorted by popularity

function icon

@babel/types.identifier is the most popular function in @babel/types (20936 examples)