How to use the createUnionTypeAnnotation function from @babel/types

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

The @babel/types.createUnionTypeAnnotation method creates a union type annotation in an Abstract Syntax Tree (AST) for Babel.

55
56
57
58
59
60
61
62
63
64
65
66
67


  if (t.createFlowUnionType) {
    return t.createFlowUnionType(types);
  }


  return t.createUnionTypeAnnotation(types);
}


function getConstantViolationsBefore(binding, path, functions) {
  const violations = binding.constantViolations.slice();
fork icon0
star icon1
watch icon1

+ 9 other calls in file

119
120
121
122
123
124
125
126
127
128
129
130
131


  if (t.createFlowUnionType) {
    return t.createFlowUnionType(argumentTypes);
  }


  return t.createUnionTypeAnnotation(argumentTypes);
}


function ConditionalExpression() {
  const argumentTypes = [this.get("consequent").getTypeAnnotation(), this.get("alternate").getTypeAnnotation()];
fork icon0
star icon0
watch icon0

How does @babel/types.createUnionTypeAnnotation work?

@babel/types.createUnionTypeAnnotation is a function in the @babel/types module of Babel that creates an AST node representing a union type annotation, which is a type annotation that can be one of several different types. It takes an array of type annotations as its argument and returns an AST node representing the union type annotation.

Ai Example

1
2
3
4
5
6
7
8
const t = require("@babel/types");

const unionType = t.createUnionTypeAnnotation([
  t.stringTypeAnnotation(),
  t.numberTypeAnnotation(),
]);

console.log(unionType);

In this example, @babel/types.createUnionTypeAnnotation is used to create a union type annotation that consists of a string type annotation and a number type annotation. The resulting union type annotation is then logged to the console.

Other functions in @babel/types

Sorted by popularity

function icon

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