Skip to content

Instantly share code, notes, and snippets.

@baetheus
Created June 12, 2019 20:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save baetheus/aaf5170c2645f02c3993f8c36441836b to your computer and use it in GitHub Desktop.
Building typescript syntax programatically
import * as ts from 'typescript';
// import { Option as Option } from 'fp-ts/lib/Option';
const a = ts.createImportDeclaration(
undefined,
undefined,
ts.createImportClause(
undefined,
ts.createNamedImports([
ts.createImportSpecifier(
ts.createIdentifier('Option'),
ts.createIdentifier('Option')
)
])
),
ts.createStringLiteral('fp-ts/lib/Option')
)
// export type BulkAddRemoveTagsRequest = {
// entryKeys: Array<string>;
// addTags: Option<Array<string>>;
// removeTags: Option<Array<string>>;
// };
const b = ts.createTypeAliasDeclaration(
undefined,
[ts.createModifier(ts.SyntaxKind.ExportKeyword)],
ts.createIdentifier('BulkAddRemoveTagsRequest'),
undefined,
ts.createTypeLiteralNode([
ts.createPropertySignature(
undefined,
ts.createIdentifier('entryKeys'),
undefined,
ts.createTypeReferenceNode(ts.createIdentifier('Array'), [
ts.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
]),
undefined
),
ts.createPropertySignature(
undefined,
ts.createIdentifier('addTags'),
undefined,
ts.createTypeReferenceNode(ts.createIdentifier('Option'), [
ts.createTypeReferenceNode(ts.createIdentifier('Array'), [
ts.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
])
]),
undefined
),
ts.createPropertySignature(
undefined,
ts.createIdentifier('removeTags'),
undefined,
ts.createTypeReferenceNode(ts.createIdentifier('Option'), [
ts.createTypeReferenceNode(ts.createIdentifier('Array'), [
ts.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
])
]),
undefined
)
])
)
// export const BulkAddRemoveTagsRequestIO = ioType({
// entryKeys: ioArray(ioString),
// addTags: createOptionFromNullable(ioArray(ioString), 'addTags'),
// removeTags: createOptionFromNullable(ioArray(ioString), 'removeTags'),
// });
const c = ts.createVariableStatement(
[ts.createModifier(ts.SyntaxKind.ExportKeyword)],
ts.createVariableDeclarationList(
[
ts.createVariableDeclaration(
ts.createIdentifier('BulkAddRemoveTagsRequestIO'),
undefined,
ts.createCall(ts.createIdentifier('ioType'), undefined, [
ts.createObjectLiteral(
[
ts.createPropertyAssignment(
ts.createIdentifier('entryKeys'),
ts.createCall(ts.createIdentifier('ioArray'), undefined, [
ts.createIdentifier('ioString')
])
),
ts.createPropertyAssignment(
ts.createIdentifier('addTags'),
ts.createCall(
ts.createIdentifier('createOptionFromNullable'),
undefined,
[
ts.createCall(ts.createIdentifier('ioArray'), undefined, [
ts.createIdentifier('ioString')
]),
ts.createStringLiteral('addTags')
]
)
),
ts.createPropertyAssignment(
ts.createIdentifier('removeTags'),
ts.createCall(
ts.createIdentifier('createOptionFromNullable'),
undefined,
[
ts.createCall(ts.createIdentifier('ioArray'), undefined, [
ts.createIdentifier('ioString')
]),
ts.createStringLiteral('removeTags')
]
)
)
],
false
)
])
)
],
ts.NodeFlags.Const
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment