This package provides conversion functions between core-types
and TypeScript.
You probably don't want to use this package directly, but rather typeconv
which uses this package to convert between TypeScript, JSON Schema and GraphQL.
Other conversion packages:
core-types-json-schema
core-types-graphql
core-types-suretype
(which is also using this package)
Since 4.0, this package is pure ESM and requires Node 14.13.1 or later.
There are two conversion functions, convertCoreTypesToTypeScript
and convertTypeScriptToCoreTypes
, both returning a wrapped value, of the type ConversionResult
.
import { convertCoreTypesToTypeScript } from 'core-types-ts'
let doc; // This core-types document comes from somewhere
const { data: tsSourceCode } = convertCoreTypesToTypeScript( doc );
You can provide options as a second argument fn the type:
interface ToTsOptions
{
warn?: WarnFunction;
filename?: string;
sourceFilename?: string;
useUnknown?: boolean;
declaration?: boolean;
userPackage?: string;
userPackageUrl?: string;
noDisableLintHeader?: boolean;
noDescriptiveHeader?: boolean;
namespaces?: 'ignore'| 'dot'| 'underscore'| 'all';
unsupported?: 'ignore' | 'warn' | 'error';
}
These options are all optional.
warn
: A function callback to be used for warnings, defaults toconsole.warn
.filename
The filename to be written to.
This is a hint, no file will be written by the conversion function.sourceFilename
: The name of the source file from which the core-types comes.useUnknown
: Useunknown
rather thanany
for any-types.declaration
: Write a declaration file, where e.g. "export interface" becomes "export declare interface".userPackage
: The name of the package using this package.userPackageUrl
: The url to the package using this package.noDisableLintHeader
: Prevent writing the "disable linting" comment.noDescriptiveHeader
: Do no write a top-level descriptive comment about the auto-generated filenamespaces
: Try to reconstruct namespacesignore
: Don't try to reconstruct namespaces (default)dot
: Split names by dot (.) as namespaces for top-level typesunderscore
: Split names by underscore (_) as namespaces for top-level typesall
: Split by dot (.) and/or underscores (_) as namespaces for top-level types
unsupported
: What to do when detecting an unsupported typeignore
: Ignore (skip) typewarn
: Ignore type, but warn (default)error
: Throw an error
The warn
function is of type WarnFunction
from core-types
, meaning it takes a message as string, and an optional second argument of type CoreTypesErrorMeta
, also from core-types
.
import { convertTypeScriptToCoreTypes } from 'core-types-ts'
let sourceCode; // This source code comes from somewhere
const { data: doc } = convertTypeScriptToCoreTypes( sourceCode );
An optional second argument can be provided on the form
interface FromTsOptions
{
warn?: WarnFunction;
namespaces?: 'ignore' | 'hoist' | 'join-dot' | 'join-underscore';
nonExported?: 'fail' | 'ignore' | 'include' | 'inline' | 'include-if-referenced';
unsupported?: 'ignore' | 'warn' | 'error';
}
warn
: The same warn function as in CoreTypesToGraphqlOptionsnamespaces
: How to deal with namespaces *ignore
: Ignore namespaces entirely (default)hoist
: Hoist types inside namespaces to top-level, so that the types are included, but without their namespace. This can cause conflicts, in which case deeper declarations will be dropped in favor of more top-level declarations. Same-level will be exported non-deterministically.join-dot
: Join namespaces and types with a dot (.)join-underscore
: Join namespaces and types with an underscore (_)
nonExported
: How to handle references to non-exported typesfail
: Fail conversion with an Errorignore
: Don't include non-exported types, but allow references to theminclude
: Include non-exported typesinline
: Don't include non-exported types, inline them if necessary.
Will throw an Error if the inlined types have cyclic dependencies.include-if-referenced
: Include non-exported types only if they are referenced
from exported types (default)
unsupported
: What to do when detecting an unsupported typeignore
: Ignore (skip) type (default)warn
: Ignore type, but warnerror
: Throw an error