How to use the unionTypeAnnotation function from @babel/types
Find comprehensive JavaScript @babel/types.unionTypeAnnotation code examples handpicked from public code repositorys.
@babel/types.unionTypeAnnotation is a function provided by the @babel/types module that creates an AST node representing a union type annotation in a JavaScript program.
19 20 21 22 23 24 25 26 27 28
ast.leadingComments = this.buildComments(object.description || '', object.links) return ast } buildUnionOfTypes(types/*: Array<string>*/) { return bt.unionTypeAnnotation(types.map((name) => this.buildNativeType(name))) } buildUnionOfTypesFromLiteral(types/*: string*/, separator/*: string*/) { const reduced = types.split(separator).reduce((r, v) => [...r, v.trim()], [])
+ 23 other calls in file
GitHub: deecewan/jsroutes-types
70 71 72 73 74 75 76 77 78 79
const validValueId = t.identifier('ValidValue'); const validValueType = t.declareTypeAlias( validValueId, null, t.unionTypeAnnotation([ t.stringTypeAnnotation(), t.numberTypeAnnotation(), t.booleanTypeAnnotation(), stringableValue,
+ 30 other calls in file
How does @babel/types.unionTypeAnnotation work?
@babel/types.unionTypeAnnotation
is a function that creates an AST node of type UnionTypeAnnotation
, which represents a union of two or more types in a JavaScript program.
The UnionTypeAnnotation
node has a single property, types
, which is an array of type annotations that represent the individual types in the union.
For example, the following code creates a UnionTypeAnnotation
node that represents the union of two types, string
and number
:
javascriptconst t = require('@babel/types');
const unionType = t.unionTypeAnnotation([
t.stringTypeAnnotation(),
t.numberTypeAnnotation()
]);
console.log(unionType);
// outputs: { type: 'UnionTypeAnnotation', types: [ { type: 'StringTypeAnnotation' }, { type: 'NumberTypeAnnotation' } ] }
In this example, we're using @babel/types
to create an AST node for a union type that includes string
and number
types. We create a UnionTypeAnnotation
node by calling t.unionTypeAnnotation()
and passing in an array of type annotations, which we create using other functions provided by @babel/types
.
The resulting UnionTypeAnnotation
node has a types
property that is an array of two type annotation nodes: a StringTypeAnnotation
node and a NumberTypeAnnotation
node.
This AST node can then be used as part of a larger AST tree representing a JavaScript program, and can be transformed or manipulated using other functions provided by the @babel/types
module.
157 158 159 160 161 162 163 164 165 166
function unionTypeAnnotation(types: $ReadOnlyArray<BabelAST>): BabelAST { invariant( types.length > 0, 'RelayFlowBabelFactories: cannot create a union of 0 types', ); return types.length === 1 ? types[0] : t.unionTypeAnnotation(types); } module.exports = { anyTypeAlias,
+ 3 other calls in file
707 708 709 710 711 712 713 714 715 716 717 718
if (!noFutureProofEnums) { values.push('%future added value'); } return exportType(name, t.unionTypeAnnotation(values.map(function (value) { return t.stringLiteralTypeAnnotation(value); }))); }); } // If it's a @refetchable fragment, we generate the $fragmentRef in generated
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12
const t = require("@babel/types"); const unionType = t.unionTypeAnnotation([ t.stringTypeAnnotation(), t.numberTypeAnnotation(), ]); const variableDeclaration = t.variableDeclaration("const", [ t.variableDeclarator(t.identifier("myVariable"), unionType), ]); console.log(variableDeclaration);
In this example, we're using @babel/types to create an AST node for a variable declaration with a union type annotation. We create a UnionTypeAnnotation node by calling t.unionTypeAnnotation() and passing in an array of two type annotations: a StringTypeAnnotation node and a NumberTypeAnnotation node. We then create a VariableDeclaration node using t.variableDeclaration(), passing in 'const' as the kind of the variable declaration, and an array of VariableDeclarator nodes. In this case, we only have one VariableDeclarator, which declares a variable called myVariable with the union type annotation we created earlier. Finally, we log the resulting VariableDeclaration node to the console. The output should be: javascript Copy code
920 921 922 923 924 925 926 927 928 929
if (!noFutureProofEnums) { values.push('%future added value'); } return exportType( name, t.unionTypeAnnotation( values.map(value => t.stringLiteralTypeAnnotation(value)), ), ); });
104 105 106 107 108 109 110 111 112 113 114 115
return t.numberTypeAnnotation(); } else if (left.isBaseType("string") || right.isBaseType("string")) { return t.stringTypeAnnotation(); } return t.unionTypeAnnotation([t.stringTypeAnnotation(), t.numberTypeAnnotation()]); } } function LogicalExpression() {
701 702 703 704 705 706 707 708 709 710
if (_existing) { if (_existing.node.value.type === 'UnionTypeAnnotation') { _existing.node.value.types.push(property.node.value); } else { _existing.node.value = t.unionTypeAnnotation([_existing.node.value, property.node.value]); } } else { seen.set(_propertyName, property); properties.push(property);
+ 3 other calls in file
@babel/types.identifier is the most popular function in @babel/types (20936 examples)