How to use the isTSTypeReference function from @babel/types
Find comprehensive JavaScript @babel/types.isTSTypeReference code examples handpicked from public code repositorys.
The @babel/types.isTSTypeReference function checks if a node is a TypeScript type reference.
668 669 670 671 672 673 674 675 676 677
function isReactComponent(path) { if (path.isFunction()) { if ( path.node.returnType && t.isTSTypeReference(path.node.returnType.typeAnnotation) && t.isTSQualifiedName(path.node.returnType.typeAnnotation.typeName) && t.isIdentifier(path.node.returnType.typeAnnotation.typeName.left, {name: 'JSX'}) && t.isIdentifier(path.node.returnType.typeAnnotation.typeName.right, {name: 'Element'}) ) {
+ 461 other calls in file
51 52 53 54 55 56 57 58 59 60
} const value = node.literal.value; const type = typeof value; return type.replace(/^./, (c) => c.toUpperCase()); } if (t.isTSAnyKeyword(node) || t.isTSTypeReference(node) || t.isTSTypeLiteral(node)) { // field: any; // field: MyInterface; // field: { prop: string };
+ 25 other calls in file
How does @babel/types.isTSTypeReference work?
@babel/types.isTSTypeReference is a function in the @babel/types package that returns true if the given argument is a Typescript type reference node, and false otherwise. It checks whether the given node has a type property that is equal to TSTypeReference.
Ai Example
1 2 3 4 5 6 7 8
const t = require("@babel/types"); const node = t.tsTypeReference( t.identifier("MyType"), t.tsTypeParameterInstantiation([t.tsStringKeyword()]) ); console.log(t.isTSTypeReference(node)); // true
In this example, @babel/types.isTSTypeReference is used to check whether the node object is a TypeScript type reference. The function returns true because node was created using t.tsTypeReference, which creates a TypeScript type reference node.
@babel/types.identifier is the most popular function in @babel/types (20936 examples)