How to use the objectTypeProperty function from @babel/types

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

@babel/types.objectTypeProperty is a function used in Babel that creates an object type property node for an abstract syntax tree (AST) representing a type definition in JavaScript.

102
103
104
105
106
107
108
109
110
111
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),
      {
        optional: Boolean(field.allowNull),
      },
    );
fork icon23
star icon115
watch icon4

+ 7 other calls in file

58
59
60
61
62
63
64
65
66

  return ast
}

buildField(object/*: Field*/) {
  const ast = bt.objectTypeProperty(
    bt.identifier(object.name),
    this.buildNativeType(object.type)
  )
fork icon9
star icon40
watch icon4

+ 11 other calls in file

How does @babel/types.objectTypeProperty work?

@babel/types.objectTypeProperty is a function that creates an object type property node for an abstract syntax tree (AST) representing a type definition in JavaScript.

In JavaScript, type definitions can be used to describe the shape of objects and other values in the program. An object type property is a key-value pair in a type definition, where the key is a string and the value describes the type of the property value.

When called, @babel/types.objectTypeProperty creates an object type property node with the given key and value type annotations, and any additional options specified. The resulting node can be inserted into an AST and used to represent the type property in JavaScript code.

For example, suppose we have the following type definition in JavaScript code:

javascript
type Person = { name: string, age: number };

This type definition could be represented in an AST as a TypeAlias node, with two ObjectTypeProperty nodes representing the name and age properties:

vbnet
TypeAlias └── ObjectTypeAnnotation ├── ObjectTypeProperty (key="name", value=StringTypeAnnotation) └── ObjectTypeProperty (key="age", value=NumberTypeAnnotation)

In this example, the @babel/types.objectTypeProperty function would be used to create the ObjectTypeProperty nodes for the name and age properties, specifying the appropriate key and value type annotations. When the AST is generated, these ObjectTypeProperty nodes will be used to represent the type properties in the resulting JavaScript code.

45
46
47
48
49
50
51
52
53
54
  return null
}
const name = selection.name.value
if (!type.fieldsByName[name]) {
  console.warn('Unknown field: ' + name)
  return t.objectTypeProperty(t.identifier(name),
  t.anyTypeAnnotation()
    )
}
const typeField = type.fieldsByName[name];
fork icon1
star icon4
watch icon2

+ 15 other calls in file

58
59
60
61
62
63
64
65
66
67
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
15
16
17
18
19
20
const { types: t } = require("@babel/core");

module.exports = function (babel) {
  const { types: t } = babel;

  return {
    visitor: {
      TypeAlias(path) {
        if (path.node.id.name === "Person") {
          // Add a new "height" property to the Person type definition
          const newProperty = t.objectTypeProperty(
            t.identifier("height"),
            t.numberTypeAnnotation()
          );
          path.get("right").pushContainer("properties", newProperty);
        }
      },
    },
  };
};

In this example, we are creating a Babel plugin that transforms a type definition for a Person object by adding a new height property with a number type annotation. The t.objectTypeProperty function from @babel/types is used to create a new ObjectTypeProperty node representing the height property with the key t.identifier('height') and the value type annotation t.numberTypeAnnotation(). We use path.get('right').pushContainer() to add the new ObjectTypeProperty node to the properties array of the ObjectTypeAnnotation node that represents the Person type definition. After this transformation, the resulting type definition would look like this: javascript Copy code

148
149
150
151
152
153
154
155
156
157
state.generatedInputObjectTypes[typeIdentifier] = 'pending';
const fields = schema.getFields(schema.assertInputObjectType(type));
const props = fields.map(fieldID => {
  const fieldType = schema.getFieldType(fieldID);
  const fieldName = schema.getFieldName(fieldID);
  const property = t.objectTypeProperty(
    t.identifier(fieldName),
    transformInputType(schema, fieldType, state),
  );
  if (
fork icon0
star icon1
watch icon0

+ 15 other calls in file

138
139
140
141
142
143
144
145
146

/**
 * +KEY: VALUE
 */
function readOnlyObjectTypeProperty(key: string, value: BabelAST): $FlowFixMe {
  const prop = t.objectTypeProperty(t.identifier(key), value);
  prop.variance = t.variance('plus');
  return prop;
}
fork icon0
star icon1
watch icon3

+ 3 other calls in file

596
597
598
599
600
601
602
603
604
605
606
607
  });
}


function generateInputVariablesType(node, state) {
  return exportType("".concat(node.name, "Variables"), exactObjectTypeAnnotation(node.argumentDefinitions.map(function (arg) {
    var property = t.objectTypeProperty(t.identifier(arg.name), transformInputType(arg.type, state));


    if (!(arg.type instanceof GraphQLNonNull)) {
      property.optional = true;
    }
fork icon0
star icon0
watch icon2

Other functions in @babel/types

Sorted by popularity

function icon

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