Created
June 12, 2019 20:58
-
-
Save baetheus/aaf5170c2645f02c3993f8c36441836b to your computer and use it in GitHub Desktop.
Building typescript syntax programatically
This file contains hidden or 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 * 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