Is it possible to run tsc on a whole directory in Typescript?

It's quite interesting that I couldn't find this information in the documentation - is there a simple way to instruct tsc to compile all files within a directory and its subdirectories without setting up a complete tsconfig.json configuration?

Answer №1

As far as I am aware, there is no way to use tsc from the command line without a tsconfig.json file unless you specify each file individually like this:

tsc MyFile1.ts MyFile2.ts MyFile3.ts

However, it seems that you can simply create an empty tsconfig.json file (just containing {}) in the root of your TypeScript directory and it will achieve the desired result. According to https://github.com/Microsoft/TypeScript/wiki/tsconfig.json:

If no "files" property is present in a tsconfig.json file, the compiler will automatically include all TypeScript (*.ts or *.tsx) files in the current directory and its subdirectories.

Answer №2

Using globs can simplify your TypeScript compilation command.

tsc x/file1.ts x/file2.ts x/file3.ts

This command is essentially the same as using a glob pattern:

tsc x/*.ts

Answer №3

In the event that you come across a tsconfig.json file in your project directory, simply input

tsc and all of your typescript files will be compiled within the same folder. If for some reason you do not have a tsconfig.json file, you can create one by typing:

tsc -init

Answer №4

While some may consider it excessive, I recently created a PowerShell function to be used in Visual Studio Code as a task;

function CompileTypeScriptFiles($folder) {
    $tsFiles = Get-ChildItem $folder -Filter "*ts" -Recurse
    $tsFiles | ForEach-Object {
        $tsFile = $_.FullName;
        $options = $tsFile + " --outDir js --sourceMap"
        Start-Process "tsc" $options 
    }
}

Answer №5

For those utilizing Windows, the for loop is essential:

for %f in (./path/*.ts) do npx tsc "./path/%f" --lib es2018 --outDir ./path/bin

Remember to double up on the % if utilizing it within a bat file:

for %%f in (./path/*.ts) do npx tsc "./path/%%f" --lib es2018 --outDir ./path/bin

Answer №6

All types of files can be constructed, including:

tsc ./**/*.ts

Answer №7

For those using Linux/Mac, a workaround is to execute the following command:

pushd dir && tsc && popd

It appears that a tsconfig.json file is required in the specified directory.

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

Injecting JavaScript page level variable into an integrated Angular2 RC 1 application

To keep this brief, the issue I'm facing is an extension of a Stack Overflow question regarding passing page-level variables into an Angular 2 app service. Following Gunter's advice from the provided SO question, I attempted to pass a JavaScript ...

Angular 5 is unable to access the value of a form control when the name attribute is not specified

Snippet of HTML code: <li class="dropdownfilter" *ngIf="this.arr.inclues('Male')" (click)="getValueGender('Male',1,)" [(ngModel)]="M"><a>Male</a></li> I encountered the following error: ERROR Error: No value a ...

What is the best way to pause function execution until a user action is completed within a separate Modal?

I'm currently working on a drink tracking application. Users have the ability to add drinks, but there is also a drink limit feature in place to alert them when they reach their set limit. A modal will pop up with options to cancel or continue adding ...

Getting the current page name within the app.component.ts file in Ionic 3 - a simple guide

Is it possible to retrieve the current active page name within the app.component.ts file in Ionic without having to add code to any other pages? ...

In Typescript, what is a function that can return multiple types separated by commas known as?

As someone who is new to Typescript, I recently came across the following syntax: interface Foo { // what does age signify and // what if it is not optional i.e () => string, age:number; (): () => string, age?: number; } From what I ...

Can the routing module be utilized to invoke functions that retrieve the current routing value?

When working with Angular, I have the need to call certain functions that will return a value based on the current page routing. These functions are currently located within the element that needs to be changed by the route's component. I know how to ...

What is causing the error "has no properties in common with" in this wrapped styled-component?

When looking at the following code, Typescript is flagging an error on <HeaderInner>: [ts] Type '{ children: Element; }' has no properties in common with type 'IntrinsicAttributes & Pick & Partial>, "className"> & ...

Typescript displays an error message when attempting to assign a list of string variants to a defined type

Encountering an interesting TS error in the code snippet below: interface Bar { pictureType: "X" | "Y" } interface RT { output: Bar[] } const func = (): RT => { const list = [{ pictureType: 'X', }] r ...

TypeScript encountered an error (TS2403) stating that subsequent variable declarations must have matching types

Encountered an issue with my typings.d.ts file Error TS2403: Subsequent variable declarations must have the same type. Variable 'module' is expected to be of type 'NodeModule', but is currently of type '{id:string}'. declare ...

Jest assertions encountering type errors due to Cypress

After using react-testing-library and @testing-library/jest-dom/extend-expect, I decided to install Cypress. However, I now face Typescript errors on all my jest matchers: Property 'toEqual' doesn't exist on type 'Assertion'. Did ...

Can someone explain the rationale behind this syntax and how it functions effectively?

Can you explain the functionality of this code snippet? const content : string = functionThatReturnsAString(); const blob = new Blob([content]); What does the [string] represent in this code? What is the output, and which constructor can it be passed as ...

Problem encountered when attempting to save log information to a file using typescript-logging in Angular 11

Seeking insight on how to log information, debugging details, and error messages into a file (such as app.log or error.log) using typescript-logging for Angular. Alternatively, is there a more efficient method to log debug/info/errors in Angular 11? I have ...

What is the best way to run tests on TypeScript-built node apps utilizing threads?

My server is written in TypeScript, built with rollup and runs on node. Currently, I have numerous tests including unit tests that import and test specific entities, as well as end-to-end tests that start the server (with database and other components) and ...

Eliminating null values from a multidimensional array

Is there a way to remove the array elements cctype, cctypologycode, and amount if they are empty? What would be the most efficient approach? { "ccInput": [ { "designSummaryId": 6, "CCType": "A", "CCTypologyCode": "A", "Amount ...

What is the best way to add a custom typeguard to an object in TypeScript?

Looking to implement a type guard as an object method? I have an array of objects with similar data structures, but crucial differences that need to be checked and guarded using TypeScript. interface RangeElement extends Element { value: number; } inter ...

In Typescript, you can easily group a string into sections that consist of digits like 345-67, along with text containing a

I have a string that looks like this: "[111-11] text here with digits 111, [222-22-22]; 333-33 text here" and I am trying to parse it so that I can extract the code [111-11], [222-22-22], [333-33] along with their respective text descriptions. The challeng ...

Using Angular to access HTML content through the .ts file

Is there a way to retrieve the value of the input field [newUser] when clicking on the button and executing the action [onAddUser()] in the .ts file? <input type="text" ng-model="newUser" style="text-align:center"/> <button (cl ...

Display or hide an image in Angular when a button is clicked

I am facing an issue in my code where I want to show and hide an image through a link when a button is clicked. Currently, when I click the button, the image opens in a new tab in Chrome instead of displaying it on my app's page. I need to link the UR ...

When running ng build, the DefinitelyTyped package @types/async encounters issues

Previously, we built a website last year using the following versions: node v6.9.1 npm v3.10.8 angular-cli v1.0.0-beta.24 typescript v2.1.4 The application was successfully created by the previous developer. However, after setting up these versions and ...

Can you explain the use of parentheses in a typescript type when defining a key?

Could someone provide an instance of an object that matches the TypeScript type below? I'm a bit confused because I've only worked with interfaces before and have only seen square brackets. type Hash = { (data: Uint8Array): Uint8Array blockLe ...