benjamn / ast-types

Esprima-compatible implementation of the Mozilla JS Parser API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Simple example of how to use the builders and namedTypes in Typescript.

IAlwaysBeCoding opened this issue · comments

Being new to Typescript I'm having a lot of problems trying to figure out how to use ast-types. I have been using ast-types with plain nodejs by following the example on the docs.

example


var n = require("ast-types").namedTypes;
var b = require("ast-types").builders;

var fooId = b.identifier("foo");

However, when I try to use it on Typescript I can't seem to figure out how to get the builders and namedTypes imported with Typescript.

I've tried these combinations

#This example only exports the types.
import {Builders, NamedType} from "ast-types"

#I tried this combination to get the builders, and no luck.
import * as astTypes from "ast-types"
console.log(astTypes.builders)

I really just want a simple easy 2-5 line example that shows me how to use ast-types in Typescript as I'm already doing it on Nodejs.

It seems that types and instance are not matched.

import  types from 'ast-types';
// type is right ,but types is undefined
// TypeError: Cannot read property 'builders' of undefined
console.log(types.builders);