core-types ↔ TypeScript Interface Conversion
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.
Common errors
-
SyntaxError: Cannot use import statement outside a module
cause Attempting to use `import` syntax in a CommonJS environment, or trying to `require()` this ESM-only package.fixEnsure your project is configured for ESM (e.g., `"type": "module"` in `package.json`) and use `import` statements. If trying to `require()`, switch to `import`. -
TypeError: (0, _core_types_ts.convertCoreTypesToTypeScript) is not a function
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.fixVerify 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. -
Error: Not able to resolve type for ...
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.fixReview 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`).
Warnings
- breaking Version 4.0.0 removed CommonJS support, making the package pure ESM. Direct `require()` statements will fail.
- breaking Version 3.0.0 dropped support for Node.js 12.
- breaking 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.
- gotcha 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.
Install
-
npm install core-types-ts -
yarn add core-types-ts -
pnpm add core-types-ts
Imports
- convertCoreTypesToTypeScript
const { convertCoreTypesToTypeScript } = require('core-types-ts')import { convertCoreTypesToTypeScript } from 'core-types-ts' - convertTypeScriptToCoreTypes
const convertTypeScriptToCoreTypes = require('core-types-ts').convertTypeScriptToCoreTypesimport { convertTypeScriptToCoreTypes } from 'core-types-ts' - ToTsOptions
import type { ToTsOptions } from 'core-types-ts'
Quickstart
import { convertCoreTypesToTypeScript, convertTypeScriptToCoreTypes } from 'core-types-ts';
import type { NodeType } from 'core-types';
// Example: Convert core-types document to TypeScript source code
const coreTypeDoc: NodeType = {
type: 'object',
properties: {
id: { node: { type: 'string' }, required: true },
name: { node: { type: 'string' }, required: false },
age: { node: { type: 'number' }, required: false }
},
additionalProperties: false
};
const { data: tsSourceCode } = convertCoreTypesToTypeScript(coreTypeDoc, {
filename: 'example.ts',
useUnknown: true,
declaration: true
});
console.log('--- Converted to TypeScript ---\n', tsSourceCode);
// Example: Convert TypeScript source code to core-types document
const tsSource = `
/** @core-types-name User */
export interface MyUser {
id: string;
name?: string;
age?: number;
}
`;
const { data: convertedCoreTypeDoc } = convertTypeScriptToCoreTypes(tsSource, {
namespaces: 'join-dot'
});
console.log('\n--- Converted to core-types ---\n', JSON.stringify(convertedCoreTypeDoc, null, 2));