@@ -4,13 +4,12 @@ import {getSchema, Schema} from "../util/schema.util"
44import { createCompilerHost , getSharedTypeDeclarations } from "../utils"
55
66/**
7- * Generates a schema for a given TypeScript type string.
7+ * Generates a schema for a given TypeScript type expression string.
88 *
9- * This function creates a virtual TypeScript environment, parses the provided type string
10- * (along with any provided data type definitions), and extracts the schema for the last
11- * type-related statement (TypeAlias, Interface, or Class) found in the string.
9+ * This function creates a virtual TypeScript environment, wraps the provided type expression
10+ * into a type alias 'T', and extracts the schema for it.
1211 *
13- * @param typeString - The TypeScript code containing the type definition (e.g., "type MyType = { a: string }").
12+ * @param typeString - The TypeScript type expression (e.g., "string | number" or " { a: string }").
1413 * @param dataTypes - An optional array of additional data type definitions to be included in the environment.
1514 * @returns The generated Schema for the identified type, or undefined if no valid type was found or an error occurred.
1615 */
@@ -20,7 +19,7 @@ export const getTypeSchema = (
2019) : Schema | undefined => {
2120 const fileName = "index.ts"
2221 const typeDefs = getSharedTypeDeclarations ( dataTypes )
23- const sourceCode = `${ typeDefs } \n ${ typeString } `
22+ const sourceCode = `${ typeDefs } \ntype MyType = ${ typeString } `
2423
2524 const host = createCompilerHost ( fileName , sourceCode )
2625 const program = host . languageService . getProgram ( )
0 commit comments