The purpose of tsconfig.json

Looking to utilize Typescript to automatically create a tsconfig.json file based on a runtime object. I'm seeking the definition for this object, similar to the example below:

interface TSConfigObject {
    compileOnSave?: boolean;
    files?: string[];
}

Answer №1

If you're looking to utilize some of the types from the TypeScript module, simply install it using npm (npm install typescript). While there isn't a defined type for the entire tsconfig.json file, with a bit of conditional type magic in TypeScript 2.8, you can extract the type for compiler options. The enums are readily available for direct use.

import * as ts from 'typescript' 

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: One benefit is that any modifications made to the CompilerOptions and TypeAcquisition types by the compiler team will automatically reflect in your code upon package updates. However, bear in mind that extracting certain types directly from the compiler API may expose them for potential breaking changes if modified by the compiler team in the future.

Answer №2

If you delve into the source base, you will find typings for CompilerOptions here, TypeAcquisition there, and an interface that they use after parsing a tsconfig.json over here.

Over at json.schemastore, there is a schema specifically for tsconfig.json: this way

The official website provides a detailed description of tsconfig: check it out

Taking inspiration from these sources, I have crafted the following:

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

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

// More code follows...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Leverage the Angular 2 router for sending varying values to a single component

The issue lies in the fact that the code provided below for the component AppComponent remains constant across three different routes: /, /route2, and /route3. The problem arises as the properties title and bodyHTML of the AppComponent do not update with ...

Developing a Type-inclusive TypeScript encapsulation for the first level of Array.flat() with generic functionality

I am working on creating a function that constructs an array from the input arguments, allowing for only one type or arrays of that same type. Essentially like (...items)=>items.flat(1), but with type safety and generics. It seems crucial to ensure that ...

Ensuring the proper typescript type for assigning a value in react-hook-form

I'm encountering an issue when trying to pass the function setValue() down to a child component. The error message I receive is: Type 'UseFormSetValue<Inputs>' is not assignable to type 'UseFormSetValue<Record<string, any> ...

The video in the TypeScript code within the Owl Carousel is not displaying properly - only the sound is playing. The video screen remains stationary

I recently updated my query. I am facing an issue while trying to play a video in Owl Carousal with a button click. The video plays sporadically, and most of the time it doesn't work properly. When playing without the carousel, a single video works fi ...

How can you determine the type of an argument based on the type of another argument?

Is it possible to dynamically assign value types in the const set = (key: keyof Store, value: any) function based on the keys defined in the Store interface? For instance, setting a key foo as type number and key bar as type string[]. import store from & ...

Transferring data from a parent component to a child component nestled inside a tabpanel of a tabview

In the given scenario, there is a child component nested in a tab panel defined within the parent component. This setup allows for multiple tab panels and consequently multiple instances of the child component being nested in each tab panel. The goal is to ...

Acquire request data prior to exiting function in React

I am working on a NextJS application that utilizes axios for making requests to a backend API, which requires an authentication token. To handle this, I have implemented a function that retrieves the auth token and stores it in a variable at the module-lev ...

There seems to be an issue with the Angular QuickStart project as it is not functioning properly, showing the error message "(

After following the instructions in this guide for setting up VS2015, I encountered issues when trying to run the "quick start" project or the "tour of heroes" tutorial on Google Chrome. The error message I received can be found here: Angular_QuickStart_Er ...

How can I effectively implement a withAuth higher order component (HOC) in TypeScript within Next.js?

Currently, I am working on a Next.js application and implementing the next-auth package. My goal is to develop a Higher Order Component (HOC) that can determine if the component has an active session or not. Along with this, I am utilizing eslint for code ...

The error message for Angular FormControl's minimum length validation is not showing up in the errors

My goal is to access the minlength error, but when I check all the errors, it's not there. Below is my form control title: new FormControl("", [Validators.minLength(10), Validators.required]), I expect to see both the required and minlengt ...

"Utilizing the same generic in two interface properties in Typescript necessitates the use of the

I have created an interface as follows: interface I<T>{ foo: T arr: T[] } After defining the interface, I have implemented an identity function using it: const fn = <T>({foo, arr}: I<T>) => ({foo, arr}) When calling this function l ...

Is there a way to efficiently convert several strings within an object that has been retrieved from an HTTP request into another language, and subsequently save this object with the

Is there a way for me to translate some strings in an object before storing it in another http request using the Google Translate API? I am currently getting the object from one http request and saving it with a put method. How can this be achieved? servi ...

One efficient way to iterate through an object and modify its values in a single line of code

_shop: { [key: string]: string[] } = { fruits: ['Apple', 'Orange'], vegetables: ['Tomato', 'Onions'] } Can a one-liner code be used to modify the values of _shop and return it in a specific format? The desired outp ...

What is the reasoning behind leaving out wwwroot from tsconfig?

Currently, I am working on a TypeScript project using an ASP.NET 5 template in VS.NET 2015. In the scripts/tsconfig.json file that I added, there is a default exclude section which includes: "exclude": [ "node_modules", "wwwroot" ] However, a ...

What is the mechanism behind Typescript interface scope? How can interfaces be defined globally in Typescript?

I'm diving into the world of Typescript and Deno, but I'm struggling to understand how interfaces scopes work. Here's the structure of my application: The first layer (App.ts) contains the core logic of my application. This layer can refer ...

Tips for identifying unnecessary async statements in TypeScript code?

We have been encountering challenges in our app due to developers using async unnecessarily, particularly when the code is actually synchronous. This has led to issues like code running out of order and boolean statements returning unexpected results, espe ...

Tips for parsing text responses in React to generate hyperlinks and emphasize specific words

I'm currently tackling a React project and facing an interesting challenge. I have a text response that needs to be parsed in a way that all URLs are automatically turned into clickable hyperlinks (using anchor tags). Moreover, there's a requirem ...

Consuming NATS Jetstream with multiple threads

My TypeScript application consists of multiple projects, each with its own set of microservices: server: A REST API that publishes messages to NATS for other services to update a (mongo) DB synchronizer: A process that consumes NATS messages and updates t ...

Tips for identifying functions that return objects

I'm trying to figure out how to extract the type from the object returned by a specific function. The function returns an object with two keys: X and Y. In my getItem function, I only need the type of X. I don't want to use the interface Selecte ...

Error encountered when initializing a variable within the constructor of a TypeScript file in Angular 4

This is the content of my app.component.html file PL Auth Username: Password : Generate OTP Enter OTP : Login This is the code in my app.component.ts file import { Component, OnInit } from '@angular/core' ...