{"id":15574,"library":"core-types-ts","title":"core-types ↔ TypeScript Interface Conversion","description":"core-types-ts is a utility package designed to facilitate the conversion of type definitions between the generic `core-types` document format and TypeScript interfaces/types. Currently at stable version 4.1.0, it experiences a consistent release cadence with frequent minor/patch updates and major versions driven by significant feature additions or breaking changes. While not typically used directly by end-users, it serves as a foundational component for higher-level type conversion tools like `typeconv`, which leverages it to transform types between TypeScript, JSON Schema, and GraphQL. Its key differentiator lies in providing a robust, programmatic interface for translating a simplified, JSON-compatible type system (`core-types`) into precise TypeScript declarations, including support for advanced features like namespaces, utility types (Pick, Omit, Partial), and interface heritage.","status":"active","version":"4.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/grantila/core-types-ts","tags":["javascript","type","types","generic","typescript","json"],"install":[{"cmd":"npm install core-types-ts","lang":"bash","label":"npm"},{"cmd":"yarn add core-types-ts","lang":"bash","label":"yarn"},{"cmd":"pnpm add core-types-ts","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Provides the foundational generic type system for conversion to and from TypeScript.","package":"core-types","optional":false}],"imports":[{"note":"ESM-only since v4.0.0; CommonJS `require` is no longer supported.","wrong":"const { convertCoreTypesToTypeScript } = require('core-types-ts')","symbol":"convertCoreTypesToTypeScript","correct":"import { convertCoreTypesToTypeScript } from 'core-types-ts'"},{"note":"ESM-only since v4.0.0; CommonJS `require` is no longer supported.","wrong":"const convertTypeScriptToCoreTypes = require('core-types-ts').convertTypeScriptToCoreTypes","symbol":"convertTypeScriptToCoreTypes","correct":"import { convertTypeScriptToCoreTypes } from 'core-types-ts'"},{"note":"This is a type import for configuring the core-types to TypeScript conversion.","symbol":"ToTsOptions","correct":"import type { ToTsOptions } from 'core-types-ts'"}],"quickstart":{"code":"import { convertCoreTypesToTypeScript, convertTypeScriptToCoreTypes } from 'core-types-ts';\nimport type { NodeType } from 'core-types';\n\n// Example: Convert core-types document to TypeScript source code\nconst coreTypeDoc: NodeType = {\n  type: 'object',\n  properties: {\n    id: { node: { type: 'string' }, required: true },\n    name: { node: { type: 'string' }, required: false },\n    age: { node: { type: 'number' }, required: false }\n  },\n  additionalProperties: false\n};\n\nconst { data: tsSourceCode } = convertCoreTypesToTypeScript(coreTypeDoc, {\n  filename: 'example.ts',\n  useUnknown: true,\n  declaration: true\n});\nconsole.log('--- Converted to TypeScript ---\\n', tsSourceCode);\n\n// Example: Convert TypeScript source code to core-types document\nconst tsSource = `\n  /** @core-types-name User */\n  export interface MyUser {\n    id: string;\n    name?: string;\n    age?: number;\n  }\n`;\n\nconst { data: convertedCoreTypeDoc } = convertTypeScriptToCoreTypes(tsSource, {\n  namespaces: 'join-dot'\n});\nconsole.log('\\n--- Converted to core-types ---\\n', JSON.stringify(convertedCoreTypeDoc, null, 2));","lang":"typescript","description":"Demonstrates converting both from a `core-types` document to TypeScript source code and vice-versa, showcasing basic usage and option passing for each direction."},"warnings":[{"fix":"Migrate all imports to ESM `import` statements. Ensure your project is configured for ESM, e.g., by setting `\"type\": \"module\"` in `package.json` or using `.mjs` file extensions.","message":"Version 4.0.0 removed CommonJS support, making the package pure ESM. Direct `require()` statements will fail.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Upgrade your Node.js runtime to version 14.13.1 or newer (or 16.0.0 or newer). The package explicitly requires `'>=14.13.1 || >=16.0.0'`.","message":"Version 3.0.0 dropped support for Node.js 12.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Review the `core-types-ts` v2.0.0 release notes and your code for compatibility with TypeScript 4.6. Update API calls as necessary according to the new signatures.","message":"Version 2.0.0 introduced breaking API signature changes related to TypeScript 4.6, potentially affecting consumers directly interacting with internal TypeScript AST representations or helper functions.","severity":"breaking","affected_versions":">=2.0.0 <3.0.0"},{"fix":"Consider if `typeconv` (for broader schema conversions) or other specialized tools better fit your use case, as they abstract away direct interaction with `core-types-ts`.","message":"This package is generally intended as a dependency for higher-level type conversion tools like `typeconv` and `core-types-suretype`. Direct usage might indicate a need for more comprehensive type management solutions.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Ensure your project is configured for ESM (e.g., `\"type\": \"module\"` in `package.json`) and use `import` statements. If trying to `require()`, switch to `import`.","cause":"Attempting to use `import` syntax in a CommonJS environment, or trying to `require()` this ESM-only package.","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"Verify that both your consuming code and `core-types-ts` are consistently treated as ESM modules. Check transpilation settings if using Babel or similar tools. Ensure named imports are correctly destructured.","cause":"This error often occurs in mixed CommonJS/ESM environments when a CommonJS file attempts to import an ESM module, or when incorrect destructuring is applied in transpiled code.","error":"TypeError: (0, _core_types_ts.convertCoreTypesToTypeScript) is not a function"},{"fix":"Review the TypeScript source for complex types, conditional types, or advanced Mapped Types that might not have direct `core-types` equivalents. Simplify the TypeScript declaration or use the `unsupported` option in `FromTsOptions` to `warn` or `ignore` (default is `error`).","cause":"When converting TypeScript to core-types, the parser might encounter TypeScript features or syntax it cannot currently map to the generic `core-types` model.","error":"Error: Not able to resolve type for ..."}],"ecosystem":"npm"}