Skip to content

Instantly share code, notes, and snippets.

@shadeglare
Last active March 29, 2024 11:29
Show Gist options
  • Save shadeglare/62f422aa0a12958f880a42fff337b3a3 to your computer and use it in GitHub Desktop.
Save shadeglare/62f422aa0a12958f880a42fff337b3a3 to your computer and use it in GitHub Desktop.
type Uuid = string;
import * as ts from "typescript";
const file = ts.createSourceFile("source.ts", "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
interface Dict<T> {
[key: string]: T;
}
import * as ts from "typescript";
const file = ts.createSourceFile("source.ts", "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
const dictValueIdentifier = ts.factory.createIdentifier("T");
const key = ts.factory.createParameterDeclaration(
undefined,
undefined,
undefined,
"key",
undefined,
ts.factory.createTypeReferenceNode("string")
);
const index = ts.factory.createIndexSignature(
undefined,
undefined,
[key],
ts.factory.createTypeReferenceNode(dictValueIdentifier)
);
const dictDecl = ts.factory.createInterfaceDeclaration(
undefined,
undefined,
"Dict",
[ts.factory.createTypeParameterDeclaration(dictValueIdentifier)],
undefined,
[index]
);
const result = printer.printNode(ts.EmitHint.Unspecified, dictDecl, file);
console.log(result);
import * as ts from "typescript";
const file = ts.createSourceFile("source.ts", "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
const readonlyModifier = ts.factory.createModifier(ts.SyntaxKind.ReadonlyKeyword);
const idProp = ts.factory.createPropertySignature(
[readonlyModifier],
"id",
undefined,
ts.factory.createTypeReferenceNode("string")
);
const nameProp = ts.factory.createPropertySignature(
[readonlyModifier],
"name",
undefined,
ts.factory.createTypeReferenceNode("string")
);
const commentProp = ts.factory.createPropertySignature(
[readonlyModifier],
"comment",
ts.factory.createToken(ts.SyntaxKind.QuestionToken),
ts.factory.createTypeReferenceNode("string")
)
const pricesProp = ts.factory.createPropertySignature(
[readonlyModifier],
"prices",
undefined,
ts.factory.createArrayTypeNode(ts.factory.createTypeReferenceNode("number"))
)
const productDecl = ts.factory.createInterfaceDeclaration(
undefined,
undefined,
"Product",
undefined,
undefined,
[idProp, nameProp, commentProp, pricesProp]
);
const declareModifier = ts.factory.createModifier(ts.SyntaxKind.DeclareKeyword);
const storeNamespaceBlock = ts.factory.createModuleBlock([productDecl]);
const storeNamespaceDecl = ts.factory.createModuleDeclaration(
undefined,
[declareModifier],
ts.factory.createIdentifier("Store"),
storeNamespaceBlock,
ts.NodeFlags.Namespace
);
const result = printer.printNode(ts.EmitHint.Unspecified, storeNamespaceDecl, file);
console.log(result);
declare namespace Store {
interface Product {
readonly id: string;
readonly name: string;
readonly comment?: string;
readonly prices: number[];
}
}
type StringOrNumber = string | number;
import * as ts from "typescript";
const file = ts.createSourceFile("source.ts", "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
const stringTypeReference = ts.factory.createTypeReferenceNode("string");
const uuidDecl = ts.factory.createTypeAliasDeclaration(
undefined, // decorators
undefined, // modifiers
ts.factory.createIdentifier("Uuid"), // name
undefined, // type parameters
stringTypeReference // aliased type
);
const result = printer.printNode(ts.EmitHint.Unspecified, uuidDecl, file);
console.log(result);
import * as ts from "typescript";
const file = ts.createSourceFile("source.ts", "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
const stringTypeReference = ts.factory.createTypeReferenceNode("string");
const numberTypeReference = ts.factory.createTypeReferenceNode("number");
const stringOrNumberTypeReference = ts.factory.createUnionTypeNode([
stringTypeReference,
numberTypeReference
]);
const stringOrNumberDecl = ts.factory.createTypeAliasDeclaration(
undefined, // decorators
undefined, // modifiers
ts.factory.createIdentifier("StringOrNumber"), // name
undefined, // type parameters
stringOrNumberTypeReference // aliased type
);
const result = printer.printNode(ts.EmitHint.Unspecified, stringOrNumberDecl, file);
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment