Last active
September 3, 2020 21:49
-
-
Save framp/2ea50fb6b13d6f355b35b3829dace2a4 to your computer and use it in GitHub Desktop.
Get a list of keys from a TS interface (and kill some kittens on the way)
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
// npm install --save recast babylon@next @types/node | |
import * as recast from "recast"; | |
import * as typescriptParser from "recast/parsers/typescript"; | |
import * as fs from "fs"; | |
export interface IMyTable { | |
id: number; | |
title: string; | |
createdAt: Date; | |
isDeleted: boolean; | |
} | |
const ast = recast.parse(fs.readFileSync(__filename).toString(), { | |
parser: typescriptParser, | |
}); | |
const keys = ast.program.body | |
.find( | |
(node) => | |
node?.declaration?.type === "TSInterfaceDeclaration" && | |
node?.declaration?.id?.name === "IMyTable" | |
) | |
.declaration.body.body.map((keyNode) => keyNode.key.name); | |
console.log(keys); //[ 'id', 'title', 'createdAt', 'isDeleted' ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment