Zev Spitz
Zev Spitz

Reputation: 15375

Definition for tsconfig.json

I want to write code in Typescript that generates a tsconfig.json file from a runtime object. Where can I get the definition for this object, e.g. something like the following:

interface TSConfig [
    compileOnSave?: boolean;
    files?: string[];
}

Upvotes: 3

Views: 1510

Answers (2)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 250346

You can reuse some of the types defined in the typescript module (you can get it via npm install typescript) for compiler options and type acquisition options, although there is no type defined for the full tsconfig.json, and we need to do some conditional type magic (available in typescript 2.8) to get the type for compiler options. The enums are exported, so you can just use them directly.

import * as ts from 'typescript' // Import will be elided as long as we only use types from it, so we don't have the compiler code  loaded at runtime

type CompilerOptions = typeof ts.parseCommandLine extends (...args: any[])=> infer TResult ? 
    TResult extends { options: infer TOptions } ? TOptions : never : never;
type TypeAcquisition = typeof ts.parseCommandLine extends (...args: any[])=> infer TResult ? 
    TResult extends { typeAcquisition?: infer TTypeAcquisition } ? TTypeAcquisition : never : never;

interface TsConfig {

    compilerOptions: CompilerOptions;
    exclude: string[];
    compileOnSave: boolean;
    extends: string;
    files: string[];
    include: string[];
    typeAcquisition: TypeAcquisition
}

Note This has the advantage that changes made to the CompilerOptions and TypeAcquisition types by the compiler team will be reflected in your code when you update the package. The disadvantage is that it extracts some types from the compiler API which probably were not directly exposed for a reason (although since they are part of the result of ts.parseCommandLine if the compiler team would change them, it would be a breaking API change)

Upvotes: 2

Markus
Markus

Reputation: 4149

In the source base you can finde some typings for CompilerOptions https://github.com/Microsoft/TypeScript/blob/master/lib/typescript.d.ts#L2305 , TypeAcquisition https://github.com/Microsoft/TypeScript/blob/master/lib/typescript.d.ts#L2379 and an interface that they use after they has parsed a tsconfig.json https://github.com/Microsoft/TypeScript/blob/master/lib/typescript.d.ts#L2434

On json.schemastore there exists a schema for tsconfig.json: http://json.schemastore.org/tsconfig

On the offical website there is a description for tsconfig: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

Based on them I created this:

interface MapLike<T> {
    [index: string]: T;
}

enum ModuleResolutionKind {
    Classic = 1,
    NodeJs = 2,
}

interface CompilerOptions {
    allowJs?: boolean;
    allowSyntheticDefaultImports?: boolean;
    allowUnreachableCode?: boolean;
    allowUnusedLabels?: boolean;
    alwaysStrict?: boolean;
    baseUrl?: string;
    charset?: string;
    checkJs?: boolean;
    declaration?: boolean;
    emitDeclarationOnly?: boolean;
    declarationDir?: string;
    disableSizeLimit?: boolean;
    downlevelIteration?: boolean;
    emitBOM?: boolean;
    emitDecoratorMetadata?: boolean;
    experimentalDecorators?: boolean;
    forceConsistentCasingInFileNames?: boolean;
    importHelpers?: boolean;
    inlineSourceMap?: boolean;
    inlineSources?: boolean;
    isolatedModules?: boolean;
    jsx?: JsxEmit;
    lib?: string[];
    locale?: string;
    mapRoot?: string;
    maxNodeModuleJsDepth?: number;
    module?: ModuleKind;
    moduleResolution?: ModuleResolutionKind;
    newLine?: NewLineKind;
    noEmit?: boolean;
    noEmitHelpers?: boolean;
    noEmitOnError?: boolean;
    noErrorTruncation?: boolean;
    noFallthroughCasesInSwitch?: boolean;
    noImplicitAny?: boolean;
    noImplicitReturns?: boolean;
    noImplicitThis?: boolean;
    noStrictGenericChecks?: boolean;
    noUnusedLocals?: boolean;
    noUnusedParameters?: boolean;
    noImplicitUseStrict?: boolean;
    noLib?: boolean;
    noResolve?: boolean;
    out?: string;
    outDir?: string;
    outFile?: string;
    paths?: MapLike<string[]>;
    preserveConstEnums?: boolean;
    preserveSymlinks?: boolean;
    project?: string;
    reactNamespace?: string;
    jsxFactory?: string;
    removeComments?: boolean;
    rootDir?: string;
    rootDirs?: string[];
    skipLibCheck?: boolean;
    skipDefaultLibCheck?: boolean;
    sourceMap?: boolean;
    sourceRoot?: string;
    strict?: boolean;
    strictFunctionTypes?: boolean;
    strictNullChecks?: boolean;
    strictPropertyInitialization?: boolean;
    suppressExcessPropertyErrors?: boolean;
    suppressImplicitAnyIndexErrors?: boolean;
    target?: ScriptTarget;
    traceResolution?: boolean;
    types?: string[];
    /** Paths used to compute primary types search locations */
    typeRoots?: string[];
    esModuleInterop?: boolean;
}

interface TypeAcquisition {
    enableAutoDiscovery?: boolean;
    enable?: boolean;
    include?: string[];
    exclude?: string[];
    [option: string]: string[] | boolean | undefined;
}

enum ModuleKind {
    None = 0,
    CommonJS = 1,
    AMD = 2,
    UMD = 3,
    System = 4,
    ES2015 = 5,
    ESNext = 6,
}
enum JsxEmit {
    None = 0,
    Preserve = 1,
    React = 2,
    ReactNative = 3,
}
enum NewLineKind {
    CarriageReturnLineFeed = 0,
    LineFeed = 1,
}

enum ScriptTarget {
    ES3 = 0,
    ES5 = 1,
    ES2015 = 2,
    ES2016 = 3,
    ES2017 = 4,
    ES2018 = 5,
    ESNext = 6,
    Latest = 6,
}

interface TsConfig {
    compilerOptions: CompilerOptions;
    exclude: string[];
    compileOnSave: boolean;
    extends: string;
    files: string[];
    include: string[];
    typeAcquisition: TypeAcquisition
}

Upvotes: 0

Related Questions