Last active
August 30, 2024 10:16
-
-
Save shadeglare/62f422aa0a12958f880a42fff337b3a3 to your computer and use it in GitHub Desktop.
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
type Uuid = string; |
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 * 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 }); |
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
interface Dict<T> { | |
[key: string]: T; | |
} |
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 * 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); |
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 * 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); |
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
declare namespace Store { | |
interface Product { | |
readonly id: string; | |
readonly name: string; | |
readonly comment?: string; | |
readonly prices: number[]; | |
} | |
} |
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
type StringOrNumber = string | number; |
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 * 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); |
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 * 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