Is there a way to obtain the transpiled code from the monaco editor?

Is there a way to retrieve the transpiled TypeScript code from the monaco editor? Additionally, is it feasible to utilize the TypeScript language service? I attempted the following:

monaco.languages.typescript.getTypeScriptWorker();

However, it only returns Promise<any> and I am unsure how to proceed with this information!

Answer №1

Through extensive research, it has come to light that the TypeScript LanguageService interface is somewhat exposed through a worker proxy object. This could be attributed to the service being in a separate thread where messages are sent to it, hence the necessity of using promises.

To simplify things, I carefully examined the functions exposed on the returned object and formulated this definition; hopefully, it proves beneficial to someone:

namespace ts {
    export interface IMonacoTypeScriptServiceProxy {
        // List of exposed functions
    }
}

The usage of this interface is demonstrated below:

// Sample code showcasing how to use the interface

The function

getEmitOutput(uri: string, emitOnlyDtsFiles?: boolean)
can now be invoked. The parameter uri represents the filename, converted from a monaco.Uri to a string (in monaco terminology, a model refers to a single file in the editor). Therefore:

// Example of invoking the getEmitOutput function

Calling this function will return not only the transpiled JavaScript but also any other available files! Enjoy the benefits.

Answer №2

Exciting updates have arrived in version 0.20.0. Now, the methods getTypeScriptWorker and getJavaScriptWorker provide valid types:

    export const getTypeScriptWorker: () => Promise<(...uris: Uri[]) => Promise<TypeScriptWorker>>;
    export const getJavaScriptWorker: () => Promise<(...uris: Uri[]) => Promise<TypeScriptWorker>>;

Furthermore, there has been a modification in the promise parameter.

Thanks to this update, maintaining a separate worker (proxy) type is no longer necessary.

Answer №3

We have been primarily discussing the acquisition of the JS/TS worker proxy up until this point, however, there exists a simpler solution to the original query regarding obtaining transpiled code:

    import ts from "typescript";

    By using this snippet of code, I can transmit TS code from the editor to a web worker for autonomous execution.
    

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

How can I incorporate my custom component into an Angular 12 modal using NGZorro?

I want to incorporate my own component into an nzmodal. I attempted the following: <nz-modal [(nzVisible)]="isVisible" nzTitle="Create relation" (nzOnCancel)="handleCancel()" (nzOnOk)="handleOk()"> & ...

The issue of duplicate items being stored in an array arose when inserting a new element

I am new to angular and currently working on an application that allows users to take notes and store them in a database. However, I have encountered a problem during the note addition process. When there are no existing notes in the database and I add tw ...

I am wondering why the content is not showing up when using state.map(foo => <>{foo}</>) in my code

Why does the first tsx code display the state.map properly while the second code displays nothing? Despite both pieces of code performing the same task in the same way, one list is correctly displayed while the other state.map has never rendered anything, ...

JavaScript/TypeScript Asynchronous Filtering on AsyncIterable<T>

I am currently working with an AsyncIterable variable and I am trying to apply a filter on it. Take a look at the example below (pseudo code) - class SomeClass { private SOME_CONST= "ABCDE"; private async someFunction(): Promise<strin ...

Tips for simplifying union types in typescript without resorting to strings

One of my local state variables is defined as: const [select, setSelect] = useState<'' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h'>(''); I fi ...

The module '@storybook/react' or its associated type declarations could not be located. This issue appears to be occurring specifically on Heroku

I encountered an issue when trying to deploy my app on Heroku, receiving the following message: https://i.sstatic.net/FeRcQ.png My application, built with TypeScript and React, is what I aim to deploy on Heroku. I am utilizing storybook/react within my p ...

Invoking a subclass method's constructor using a generic parameter

The code below is not passing the type-checking: type MyFunctionConstructor<T, F extends MyFunction<T>> = new ( f: (n: number) => T ) => F; class MyFunction<T> { constructor(f: (n: number) => T) { this.f = f; } f: ...

What enables typescript to be eligible for transpiling is its equivalent level of abstraction to javascript?

Transpilation is the act of converting code from one language to another with a similar level of abstraction. Can you point out some distinctive characteristics that indicate TypeScript transpires into JavaScript? ...

Altering the dimensions of radio buttons

I am a newcomer to using material-ui. I am currently working on incorporating radio buttons in a component and would like to reduce its size. While inspecting it in Chrome, I was able to adjust the width of the svg icon (1em). However, I am unsure how to a ...

Issue in Angular/Jasmine: TypeError - Attempting to access the 'labels' property of an undefined value in Angular

While writing unit tests for components, I encountered an error in the terminal when running the tests. The error message displayed was: TypeError: Cannot read property 'labels' of undefined. There were a total of 3 function errors that ...

Explaining the process of defining an object type in TypeScript and the conversion from JavaScript

Currently, I am attempting to enhance the background of a React website developed in typescript (.tsx) by incorporating particles. My approach involves utilizing the particle-bg component available at: https://github.com/lindelof/particles-bg However, whe ...

Ensure that multiple URLs within an object are properly sanitized instead of just focusing on a single

I am working with an object that contains success, summary, and detail elements, which are used to display messages in PrimeNG message component (p-messages) after a record is created. Once the record is created, I invoke the displayMessage method to set t ...

The argument '$0' provided for the pipe 'CurrencyPipe' is not valid

When retrieving data from the backend, I receive $0, but I need to display it as $0.00 in my user interface. <span [innerHTML]="session.balance | currency :'USD': true:'1.2-2'"></span> I'm encountering an issue where ...

Is it possible to dynamically check values in TypeScript?

[Summary] I am looking to dynamically expand my type in TypeScript based on an initial set of values. I want to avoid managing separate arrays/types and instead extend all strings in my type with '_max'. type ExtendedValueTypes = 'money&apos ...

Innovative solution for detecting and replacing undefined object properties in Angular 2 with TypeScript

After encountering the issue of core.umd.js:3523 ORIGINAL EXCEPTION: Cannot read property 'fullName' of undefined I realized that the Exception stemmed from a Template trying to access a specific property: {{project.collaborators["0"]["fullN ...

The art of crafting informative error log messages in Protractor using TypeScript

I am currently utilizing Protractor, written in typescript, to conduct tests on a live website. I am seeking guidance on how to generate log messages when a Protractor test fails. Presently, the only feedback received is a simple YES/NO message, as shown b ...

Generate a series of HTTP requests using an HTTP interceptor

Is it possible to initiate an http request before another ongoing http request finishes (For example, fetching a token/refresh token from the server before the current request completes)? I successfully implemented this functionality using Angular 5' ...

Leveraging the OpenLayers Map functionality within an Angular service

I am curious to learn if there is a way to utilize a service in Angular for creating an OpenLayers map and then passing that service to other components to update the map based on interactions within those components. I have outlined my approach below. Des ...

Mastering the Type Checking of React Select's onChange Event Handler

Currently, I am in the process of building a design system based on React TypeScript. For the Dropdown component, I have opted to utilize React Select to handle most of its functionality. To customize the Dropdown component, I have created a wrapper compo ...

The specified type 'Observable<string | null>' cannot be assigned to type 'string'

I am struggling with the following code: id$!: Observable<string | null>; characterDetail$!: Observable<CharacterData | undefined>; //?????? constructor( private router: ActivatedRoute, private store$: Store ) {} ngOnInit(): void ...