Created
December 13, 2017 14:56
-
-
Save alloy/7855d4f9458c63b64150a11a82970feb to your computer and use it in GitHub Desktop.
Lightning talk at the TypeScript NYC meetup: Babel 7 with TypeScript support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"dependencies": { | |
"@babel/cli": "^7.0.0-beta.34", | |
"@babel/core": "^7.0.0-beta.34", | |
"@babel/generator": "^7.0.0-beta.34", | |
"@babel/node": "^7.0.0-beta.34", | |
"@babel/preset-typescript": "^7.0.0-beta.34", | |
"@babel/types": "^7.0.0-beta.34", | |
"babylon": "^7.0.0-beta.34" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* $ ./node_modules/.bin/babel-node --presets @babel/typescript --extensions .ts wozers.ts | |
*/ | |
interface Wozers { | |
foo: string; | |
} | |
const wow: Wozers = { | |
foo: "bar" | |
} | |
console.log(wow) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* $ node babel-generator.js | |
*/ | |
const { parse } = require("babylon") | |
const generate = require("@babel/generator").default | |
const util = require('util') | |
const source = ` | |
interface Wozers { | |
foo: string; | |
} | |
const wow: Wozers = { | |
foo: "bar" | |
} | |
` | |
const ast = parse(source, { sourceType: "module", plugins: ["typescript"] }) | |
console.log(util.inspect(ast.program.body, false, null)) | |
// console.log(generate(x).code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* $ node babel-generator.js | |
*/ | |
const generate = require("@babel/generator").default | |
const t = require("@babel/types") | |
const properties = [] | |
properties.push(t.TSPropertySignature( | |
t.identifier("foo"), | |
t.TSTypeAnnotation(t.TSStringKeyword())) | |
) | |
properties.push(t.TSPropertySignature( | |
t.identifier("bar"), | |
t.TSTypeAnnotation(t.TSBooleanKeyword())) | |
) | |
const objectLiteral = t.TSTypeLiteral(properties) | |
console.log(generate(objectLiteral).code) | |
const interfaceBody = t.TSInterfaceBody(objectLiteral.members) | |
const interface = t.TSInterfaceDeclaration(t.identifier("Wozers"), null, null, interfaceBody) | |
console.log(generate(interface).code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment