GithubHelp home page GithubHelp logo

forclan / core-types-ts Goto Github PK

View Code? Open in Web Editor NEW

This project forked from grantila/core-types-ts

0.0 1.0 0.0 39 KB

core-types ⬌ TypeScript interface conversion

License: MIT License

TypeScript 100.00%

core-types-ts's Introduction

npm version downloads build status coverage status Language grade: JavaScript Node.JS version

core-types-ts

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.

See

Other conversion packages:

Contents

Usage

There are two conversion functions, convertCoreTypesToTypeScript and convertTypeScriptToCoreTypes, both returning a wrapped value, of the type ConversionResult.

core-types to TypeScript

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;
	unsupported?: 'ignore' | 'warn' | 'error';
}

These options are all optional.

  • warn: A function callback to be used for warnings, defaults to console.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: Use unknown rather than any 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 file
  • unsupported: What to do when detecting an unsupported type
    • ignore: Ignore (skip) type
    • warn: 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.

TypeScript to 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;
	nonExported?: 'fail' | 'ignore' | 'include' | 'inline' | 'include-if-referenced';
	unsupported?: 'ignore' | 'warn' | 'error';
}
  • warn: The same warn function as in CoreTypesToGraphqlOptions
  • nonExported: How to handle references to non-exported types
    • fail: Fail conversion with an Error
    • ignore: Don't include non-exported types, but allow references to them
    • include: Include non-exported types
    • inline: 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 type
    • ignore: Ignore (skip) type (default)
    • warn: Ignore type, but warn
    • error: Throw an error

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.