core-types ↔ TypeScript Interface Conversion

4.1.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Demonstrates converting both from a `core-types` document to TypeScript source code and vice-versa, showcasing basic usage and option passing for each direction.

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));

view raw JSON →