How to use the objectTypeAnnotation function from @babel/types

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

@babel/types.objectTypeAnnotation is a tool used in JavaScript for creating an object type annotation node in an Abstract Syntax Tree (AST) representation of JavaScript code.

98
99
100
101
102
103
104
105
106
107
const name = `I${bigCamelCase(
  definition.tableName,
)}`;
const attribute = ast.program.body[1];
attribute.declaration.id = t.identifier(`${name}Attributes`);
attribute.declaration.body = t.objectTypeAnnotation(
  _.map(definition.attributes, (field, key) => {
    const type = getObjectTypeAnnotation(field.type);
    return Object.assign(
      t.objectTypeProperty(t.identifier(key), type),
fork icon23
star icon115
watch icon4

+ 7 other calls in file

34
35
36
37
38
39
40
41
42
const fields = Object.keys(object.fields)
  .map((fieldName) => this.buildField(object.fields[fieldName]))
const ast = bt.declareTypeAlias(
  bt.identifier(object.name),
  undefined,
  bt.objectTypeAnnotation(fields),
)

ast.leadingComments = this.buildComments(object.description || '', object.links)
fork icon9
star icon40
watch icon4

+ 23 other calls in file

How does @babel/types.objectTypeAnnotation work?

@babel/types.objectTypeAnnotation is a function provided by the @babel/types library that is used in JavaScript for creating an object type annotation node in an Abstract Syntax Tree (AST) representation of JavaScript code. To use @babel/types.objectTypeAnnotation, developers first import the function from the @babel/types library. They can then call the function with one argument: an array of AST nodes representing the properties of the object type. Each property in the object type is represented as an AST node, which can be created using other functions in the @babel/types library. The @babel/types.objectTypeAnnotation function then combines these nodes into a single object type annotation node that can be included in an AST representation of JavaScript code. The @babel/types.objectTypeAnnotation function also supports additional options that can be passed as a second argument, such as specifying whether the object type is exact or allowing additional properties that are not specified in the object type. @babel/types.objectTypeAnnotation is a useful tool for manipulating and generating AST nodes for JavaScript code. It is often used as part of a larger process for transforming or analyzing JavaScript code programmatically.

56
57
58
59
60
61
62
63
64
65
  }).filter(Boolean)
};

const unionToFlow = (type, selections) => {
  // const type = typesByName[typeName]
  return t.objectTypeAnnotation(
    [].concat(...selections.map(selection => {
      if (selection.kind === 'Field' && selection.name.value === '__typename') {
        return [t.objectTypeProperty(t.identifier(selection.name.value), t.genericTypeAnnotation(t.identifier('string')))]
      }
fork icon1
star icon4
watch icon2

+ 15 other calls in file

57
58
59
60
61
62
63
64
65
66
const stringableId = t.identifier('Stringable');
const stringableType = t.interfaceDeclaration(
  stringableId,
  null,
  [],
  t.objectTypeAnnotation([
    t.objectTypeProperty(
      t.identifier('toString'),
      t.functionTypeAnnotation(null, [], null, t.stringTypeAnnotation()),
    ),
fork icon1
star icon3
watch icon2

+ 61 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const t = require("@babel/types");

const property1 = t.objectTypeProperty(
  t.identifier("name"),
  t.stringTypeAnnotation()
);
const property2 = t.objectTypeProperty(
  t.identifier("age"),
  t.numberTypeAnnotation()
);

const objectTypeAnnotation = t.objectTypeAnnotation([property1, property2]);

console.log(objectTypeAnnotation);

In this example, we first import the t object from the @babel/types library. We then create two AST nodes representing properties of an object type using the t.objectTypeProperty function. The first property is named "name" and has a type annotation of string, while the second property is named "age" and has a type annotation of number. We then pass these two property nodes as an array to the t.objectTypeAnnotation function, which creates an object type annotation node representing an object with the properties "name" and "age". We store the resulting object type annotation node in a variable called objectTypeAnnotation, and log it to the console using console.log(objectTypeAnnotation). When this code runs, it will create an object type annotation node representing an object with the properties "name" and "age", and log the resulting node to the console. This demonstrates how @babel/types.objectTypeAnnotation can be used to create an object type annotation node in an AST representation of JavaScript code.

130
131
132
133
134
135
136
137
138
139
      if (!(field.type instanceof GraphQLNonNull)) {
        property.optional = true;
      }
      return property;
    });
  state.generatedInputObjectTypes[typeIdentifier] = t.objectTypeAnnotation(
    props,
  );
  return t.genericTypeAnnotation(t.identifier(typeIdentifier));
} else {
fork icon0
star icon1
watch icon9

+ 9 other calls in file

207
208
209
210
211
212
213
214
215
216
            t.genericTypeAnnotation(t.identifier(refTypeName)),
          ),
        );
      }
      return unmasked
        ? t.objectTypeAnnotation(props)
        : exactObjectTypeAnnotation(props);
    }),
  );
}
fork icon0
star icon5
watch icon2

+ 3 other calls in file

29
30
31
32
33
34
35
36
37
 * |}
 */
function exactObjectTypeAnnotation(
  props: $ReadOnlyArray<BabelAST>,
): $FlowFixMe {
  const typeAnnotation = t.objectTypeAnnotation(props);
  typeAnnotation.exact = true;
  return typeAnnotation;
}
fork icon0
star icon1
watch icon3

+ 7 other calls in file

178
179
180
181
182
183
184
185
186
187
188
189
  return unionTypeAnnotation(types.map(function (props) {
    if (fragmentTypeName) {
      props.push(readOnlyObjectTypeProperty('$refType', t.genericTypeAnnotation(t.identifier(fragmentTypeName))));
    }


    return unmasked ? t.objectTypeAnnotation(props) : exactObjectTypeAnnotation(props);
  }));
}


function mergeSelection(a, b) {
fork icon0
star icon0
watch icon2

78
79
80
81
82
83
84
85
86
87
 * }
 */
function implicitInexactObjectTypeAnnotation(
  props: $ReadOnlyArray<mixed>,
): $FlowFixMe {
  return t.objectTypeAnnotation(props);
}
let inexactObjectTypeAnnotation = explicitInexactObjectTypeAnnotation;

function generate(
fork icon0
star icon0
watch icon1

+ 3 other calls in file

Other functions in @babel/types

Sorted by popularity

function icon

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