How to use the genericTypeAnnotation function from @babel/types

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

@babel/types.genericTypeAnnotation is a function in Babel that creates a new generic type annotation node.

189
190
191
192
193
194
195
196
197
198
}
if (type.indexOf('DataTypes.TEXT') > -1) {
  return t.stringTypeAnnotation();
}
if (type.indexOf('DataTypes.DATE') > -1) {
  return t.genericTypeAnnotation(t.identifier('Date'));
}
if (type.indexOf('DataTypes.FLOAT') > -1) {
  return t.numberTypeAnnotation();
}
fork icon23
star icon115
watch icon4

+ 15 other calls in file

7
8
9
10
11
12
13
14
15
16
  return `${methodName[0].toUpperCase()}${methodName.slice(1)}Payload`
}

class FlowBuilder extends AbstractJsBuilder {
  buildUnion(object/*: Union*/) {
    const variants = object.variants.map((name) => bt.genericTypeAnnotation(bt.identifier(name)))
    const ast = bt.declareTypeAlias(
      bt.identifier(object.name),
      undefined,
      bt.unionTypeAnnotation(variants)
fork icon9
star icon40
watch icon4

+ 23 other calls in file

How does @babel/types.genericTypeAnnotation work?

The @babel/types.genericTypeAnnotation is a function provided by the Babel compiler tool that creates an object node representing a generic type annotation in an Abstract Syntax Tree (AST) of a JavaScript program. The generic type annotation is used to indicate a type parameter that can be replaced with an actual type when the code is compiled or executed. The function takes an argument specifying the identifier for the type parameter and returns an object node that can be added to the AST.

64
65
66
67
68
69
70
71
72
73
      t.identifier('toString'),
      t.functionTypeAnnotation(null, [], null, t.stringTypeAnnotation()),
    ),
  ]),
);
const stringableValue = t.genericTypeAnnotation(stringableId);

const validValueId = t.identifier('ValidValue');
const validValueType = t.declareTypeAlias(
  validValueId,
fork icon1
star icon3
watch icon2

+ 61 other calls in file

202
203
204
205
206
207
208
209
210
211
types.map(props => {
  if (refTypeName) {
    props.push(
      readOnlyObjectTypeProperty(
        '$refType',
        t.genericTypeAnnotation(t.identifier(refTypeName)),
      ),
    );
  }
  return unmasked
fork icon0
star icon5
watch icon2

+ 3 other calls in file

Ai Example

1
2
3
4
5
import { types as t } from "@babel/core";

const myGenericType = t.genericTypeAnnotation(t.identifier("Array"), [
  t.numberTypeAnnotation(),
]);

In this example, myGenericType represents the generic type Array .

82
83
84
85
86
87
88
89
90
91
switch (type.name) {
  case 'Boolean':
    return t.genericTypeAnnotation(t.identifier('boolean'))
  case 'ID':
  case 'String':
    return t.genericTypeAnnotation(t.identifier('string'))
  case 'Int':
  case 'Float':
    return t.genericTypeAnnotation(t.identifier('number'))
  case 'JSONString':
fork icon1
star icon4
watch icon2

+ 63 other calls in file

226
227
228
229
230
231
232
233
234
235
types.map(props => {
  if (fragmentTypeName) {
    props.push(
      readOnlyObjectTypeProperty(
        '$refType',
        t.genericTypeAnnotation(t.identifier(fragmentTypeName)),
      ),
    );
  }
  return unmasked
fork icon0
star icon2
watch icon0

+ 15 other calls in file

92
93
94
95
96
97
98
99
100
101
  }
}

function transformGraphQLEnumType(type: GraphQLEnumType, state: State) {
  state.usedEnums[type.name] = type;
  return t.genericTypeAnnotation(t.identifier(type.name));
}

function transformInputType(type: GraphQLInputType, state: State) {
  if (type instanceof GraphQLNonNull) {
fork icon0
star icon1
watch icon9

+ 39 other calls in file

104
105
106
107
108
109
110
111
112
113
  schema: Schema,
  type: EnumTypeID,
  state: State,
) {
  state.usedEnums[schema.getTypeString(type)] = type;
  return t.genericTypeAnnotation(t.identifier(schema.getTypeString(type)));
}

function transformInputType(
  schema: Schema,
fork icon0
star icon1
watch icon0

+ 31 other calls in file

83
84
85
86
87
88
89
90
91
function declareExportOpaqueType(name: string, value: string): $FlowFixMe {
  return t.declareExportDeclaration(
    t.declareOpaqueType(
      t.identifier(name),
      null,
      t.genericTypeAnnotation(t.identifier(value)),
    ),
  );
}
fork icon0
star icon1
watch icon3

+ 7 other calls in file

326
327
328
329
330
331
332
333
334
335
  return [selection];
});
state.generatedFragments.add(node.name);
var fragmentTypes = getFragmentTypes(node.name, getRefetchableQueryPath(state, node.directives));
var refTypeName = getRefTypeName(node.name);
var refTypeDataProperty = readOnlyObjectTypeProperty('$data', t.genericTypeAnnotation(t.identifier("".concat(node.name, "$data"))));
refTypeDataProperty.optional = true;
var refTypeFragmentRefProperty = readOnlyObjectTypeProperty('$fragmentRefs', t.genericTypeAnnotation(t.identifier(getOldFragmentTypeName(node.name))));
var refType = t.objectTypeAnnotation([refTypeDataProperty, refTypeFragmentRefProperty]);
var dataTypeName = getDataTypeName(node.name);
fork icon0
star icon0
watch icon2

+ 6 other calls in file

313
314
315
316
317
318
319
320
321
322
);

const operationTypes = [
  t.objectTypeProperty(
    t.identifier('variables'),
    t.genericTypeAnnotation(t.identifier(`${node.name}Variables`)),
  ),
  t.objectTypeProperty(
    t.identifier('response'),
    t.genericTypeAnnotation(t.identifier(`${node.name}Response`)),
fork icon0
star icon0
watch icon1

+ 9 other calls in file

65
66
67
68
69
70
71
72
73
74
75
76
77


TypeCastExpression.validParent = true;


function NewExpression(node) {
  if (this.get("callee").isIdentifier()) {
    return t.genericTypeAnnotation(node.callee);
  }
}


function TemplateLiteral() {
fork icon0
star icon0
watch icon0

+ 6 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)