Code Monkey home page Code Monkey logo

tsconfig-types's Introduction

tsconfig-types

github release npm module type: esm license conventional commits typescript vitest yarn

TypeScript definitions for tsconfig.json

Contents

What is this?

This package contains TypeScript definitions for tsconfig.json files and values used in tsconfig.json files.

When should I use this?

Use this package when you need TypeScript definitions compatible with the tsconfig.json reference. Definitions exported by the typescript module itself are not compatible with values passed by users.

Install

This package is ESM only.

yarn add @flex-development/tsconfig-types

From Git:

yarn add @flex-development/tsconfig-types@flex-development/tsconfig-types
See Git - Protocols | Yarn  for details on requesting a specific branch, commit, or tag.

Use

/**
 * @file Utilities - compilerOptionsForProgram
 * @module utils/compilerOptionsForProgram
 */

import {
  ImportsNotUsedKind,
  JsxEmit,
  ModuleDetectionKind,
  ModuleKind,
  ModuleResolutionKind,
  NewLineKind,
  ScriptTarget,
  type CompilerOptions
} from '@flex-development/tsconfig-types'
import ts from 'typescript'

/**
 * Converts user compiler options to programmatic compiler options.
 *
 * @param {CompilerOptions} [compilerOptions={}] - User compiler options
 * @return {ts.CompilerOptions} Compiler options for TypeScript program
 */
const compilerOptionsForProgram = (
  compilerOptions: CompilerOptions = {}
): ts.CompilerOptions => {
  /**
   * Compiler options for TypeScript program.
   *
   * @const {ts.CompilerOptions} options
   */
  const options: ts.CompilerOptions = {}

  // user compiler options
  const {
    importsNotUsedAsValues,
    jsx,
    lib,
    module,
    moduleDetection,
    moduleResolution,
    newLine,
    target
  } = compilerOptions

  // typescript program expects declaration names to match node_modules exactly
  if (Array.isArray(lib)) {
    options.lib = lib.map((name: string) => `lib.${name}.d.ts`)
  }

  // importsNotUsedAsValues
  switch (importsNotUsedAsValues?.toLowerCase() as ImportsNotUsedKind) {
    case ImportsNotUsedKind.Error:
      options.importsNotUsedAsValues = ts.ImportsNotUsedAsValues.Error
      break
    case ImportsNotUsedKind.Preserve:
      options.importsNotUsedAsValues = ts.ImportsNotUsedAsValues.Preserve
      break
    case ImportsNotUsedKind.Remove:
      options.importsNotUsedAsValues = ts.ImportsNotUsedAsValues.Error
      break
  }

  // jsx
  switch (jsx?.toLowerCase() as JsxEmit) {
    case JsxEmit.Preserve:
      options.jsx = ts.JsxEmit.Preserve
      break
    case JsxEmit.React:
      options.jsx = ts.JsxEmit.React
      break
    case JsxEmit.ReactJSX:
      options.jsx = ts.JsxEmit.ReactJSX
      break
    case JsxEmit.ReactJSXDev:
      options.jsx = ts.JsxEmit.ReactJSXDev
      break
    case JsxEmit.ReactNative:
      options.jsx = ts.JsxEmit.ReactNative
      break
  }

  // module
  switch (module?.toLowerCase() as ModuleKind) {
    case ModuleKind.AMD:
      options.module = ts.ModuleKind.AMD
      break
    case ModuleKind.CommonJS:
      options.module = ts.ModuleKind.CommonJS
      break
    case ModuleKind.ES6:
    case ModuleKind.ES2015:
      options.module = ts.ModuleKind.ES2015
      break
    case ModuleKind.ES2020:
      options.module = ts.ModuleKind.ES2020
      break
    case ModuleKind.ES2022:
      options.module = ts.ModuleKind.ES2022
      break
    case ModuleKind.ESNext:
      options.module = ts.ModuleKind.ESNext
      break
    case ModuleKind.Node16:
      options.module = ts.ModuleKind.Node16
      break
    case ModuleKind.NodeNext:
      options.module = ts.ModuleKind.NodeNext
      break
    case ModuleKind.None:
      options.module = ts.ModuleKind.None
      break
    case ModuleKind.System:
      options.module = ts.ModuleKind.System
      break
    case ModuleKind.UMD:
      options.module = ts.ModuleKind.UMD
      break
  }

  // moduleDetection
  switch (moduleDetection?.toLowerCase() as ModuleDetectionKind) {
    case ModuleDetectionKind.Auto:
      options.moduleDetection = ts.ModuleDetectionKind.Auto
      break
    case ModuleDetectionKind.Force:
      options.moduleDetection = ts.ModuleDetectionKind.Force
      break
    case ModuleDetectionKind.Legacy:
      options.moduleDetection = ts.ModuleDetectionKind.Legacy
      break
  }

  // moduleResolution
  switch (moduleResolution?.toLowerCase() as ModuleResolutionKind) {
    case ModuleResolutionKind.Classic:
      options.moduleResolution = ts.ModuleResolutionKind.Classic
      break
    case ModuleResolutionKind.Node16:
      options.moduleResolution = ts.ModuleResolutionKind.Node16
      break
    case ModuleResolutionKind.NodeJs:
      options.moduleResolution = ts.ModuleResolutionKind.NodeJs
      break
    case ModuleResolutionKind.NodeNext:
      options.moduleResolution = ts.ModuleResolutionKind.NodeNext
      break
  }

  // newLine
  switch (newLine?.toLowerCase() as NewLineKind) {
    case NewLineKind.CarriageReturnLineFeed:
      options.newLine = ts.NewLineKind.CarriageReturnLineFeed
      break
    case NewLineKind.LineFeed:
      options.newLine = ts.NewLineKind.LineFeed
      break
  }

  // target
  switch (target?.toLowerCase() as ScriptTarget) {
    case ScriptTarget.ES3:
      options.target = ts.ScriptTarget.ES3
      break
    case ScriptTarget.ES5:
      options.target = ts.ScriptTarget.ES5
      break
    case ScriptTarget.ES6:
    case ScriptTarget.ES2015:
      options.target = ts.ScriptTarget.ES2015
      break
    case ScriptTarget.ES2016:
      options.target = ts.ScriptTarget.ES2016
      break
    case ScriptTarget.ES2017:
      options.target = ts.ScriptTarget.ES2017
      break
    case ScriptTarget.ES2018:
      options.target = ts.ScriptTarget.ES2018
      break
    case ScriptTarget.ES2019:
      options.target = ts.ScriptTarget.ES2019
      break
    case ScriptTarget.ES2020:
      options.target = ts.ScriptTarget.ES2020
      break
    case ScriptTarget.ES2021:
      options.target = ts.ScriptTarget.ES2021
      break
    case ScriptTarget.ES2022:
      options.target = ts.ScriptTarget.ES2022
      break
    case ScriptTarget.ESNext:
      options.target = ts.ScriptTarget.ESNext
      break
  }

  return { ...compilerOptions, ...options } as ts.CompilerOptions
}

export default compilerOptionsForProgram

API

Enums

Interfaces

Types

Related

Contribute

See CONTRIBUTING.md.

tsconfig-types's People

Contributors

dependabot[bot] avatar unicornware avatar

Watchers

 avatar  avatar

tsconfig-types's Issues

🐛 [`WatchOptions`] `Cannot find module '' or its corresponding type declarations`

Prerequisites

  • read documentation
  • searched open issues for existing bug report
  • searched closed issues for solution(s) or feedback

Current behavior

Type checking a file that uses WatchOptions produces an error:

node_modules/@flex-development/tsconfig-types/dist/types/watch-options.d.mts:6:62 - error TS2307: Cannot find module '' or its corresponding type declarations.

6 import type { PollingWatch, WatchDirectory, WatchFile } from '';
                                                               ~~

Found 1 error in node_modules/@flex-development/tsconfig-types/dist/types/watch-options.d.mts:6

Reproduction

  1. git clone https://github.com/flex-development/mlly

  2. cd mlly

  3. nvm use

  4. yarn --immutable

  5. yarn add @flex-development/tsconfig-types @flex-development/pathe

  6. Update src/internal/get-compiler-options.ts

    /**
     * @file Internals - getCompilerOptions
     * @module mlly/internal/getCompilerOptions
     */
    
    import pathe from '@flex-development/pathe'
    import type { CompilerOptions } from '@flex-development/tsconfig-types'
    import { loadTsconfig } from 'tsconfig-paths/lib/tsconfig-loader'
    
    /**
     * Retrieves TypeScript compiler options from `path`.
     *
     * Supports [`extends`][1].
     *
     * [1]: https://typescriptlang.org/tsconfig#extends
     *
     * @internal
     *
     * @param {string} [path=pathe.resolve('tsconfig.json')] - Tsconfig path
     * @param {(path: string) => boolean} [exists] - File existence checker
     * @param {(filename: string) => string} [read] - File content reader
     * @return {CompilerOptions} User compiler options
     */
    const getCompilerOptions = (
      path: string = pathe.resolve('tsconfig.json'),
      exists?: (path: string) => boolean,
      read?: (filename: string) => string
    ): CompilerOptions => {
      /**
       * Tsconfig object.
       *
       * @const {{ compilerOptions?: CompilerOptions } | undefined} t
       */
      const t: { compilerOptions?: CompilerOptions } | undefined = loadTsconfig(
        path,
        exists,
        read
      )
    
      return !t?.compilerOptions ? {} : t.compilerOptions
    }
    
    export default getCompilerOptions
  7. vue-tsc ./src/internal/get-compiler-options.ts --noEmit

  8. See error

Expected behavior

Type checking should not produce any errors.

Package version

2.0.0

Node.js version

v16.17.0

Package manager version

4.0.0-rc.14

Operating system (environment)

macos

Additional context

N/A

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.