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
import ts from "typescript"; | |
const code = `type A = string;`; | |
const sourceFile = ts.createSourceFile("source.ts", code, ts.ScriptTarget.Latest); | |
const host = ts.createCompilerHost({}); | |
const getSourceFile = host.getSourceFile; | |
host.getSourceFile = function (name, languageVersion) { | |
return name === sourceFile.fileName ? sourceFile : getSourceFile.call(host, name, languageVersion); | |
}; | |
const program = ts.createProgram([sourceFile.fileName], { noEmit: true }, host); |
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
import assert from "assert"; | |
import ts, { isTypeAliasDeclaration } from "typescript"; | |
function createTypeNodeFromString(type: string): ts.TypeNode { | |
const sourceFile = ts.createSourceFile("source.ts", `type A = ${type};`, ts.ScriptTarget.Latest); | |
const statement = sourceFile.statements[0]; | |
assert(isTypeAliasDeclaration(statement)); | |
return statement.type; | |
} |
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
export function packJSON(object: unknown) { | |
const fragments: object[] = []; | |
const indices = new Map<string, number>(); | |
function next(node: unknown): unknown { | |
if (node && typeof node === "object") { | |
let fragment: object; | |
if (Array.isArray(node)) { | |
fragment = node.map((el) => next(el)); | |
} else { | |
fragment = Object.entries(node).reduce<Record<string, unknown>>((obj, [key, value]) => { |
OlderNewer