Exploring Nashorn's capabilities in parsing TypeScript

Recently, I came across a discussion suggesting that Nashorn in JDK 9 should have the capability to parse TypeScript. Excited to try it out, I attempted to use it like this:

        Parser              parser = Parser.create();
        CompilationUnitTree tree   = parser.parse(file, new InputStreamReader(stream), null);

Testing it on examples from https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html, the plain JavaScript code worked fine. But when I tried the example with a type declaration, it threw an error:

jdk.nashorn.internal.runtime.ParserException: a.ts:1:23 Expected , but found :
function greeter(person:string) {
                       ^
    at jdk.scripting.nashorn/jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:297)
    at jdk.scripting.nashorn/jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:282)
    at jdk.scripting.nashorn/jdk.nashorn.internal.parser.AbstractParser.expectDontAdvance(AbstractParser.java:353)
    at jdk.scripting.nashorn/jdk.nashorn.internal.parser.AbstractParser.expect(AbstractParser.java:340)
    at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.formalParameterList(Parser.java:3991)
    at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.formalParameterList(Parser.java:3966)
    at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.functionExpression(Parser.java:3802)
    at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.statement(Parser.java:1099)
    at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.sourceElements(Parser.java:909)
    at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.program(Parser.java:844)
    at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.parse(Parser.java:325)
    at jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.parse(Parser.java:285)
    at jdk.scripting.nashorn/jdk.nashorn.api.tree.ParserImpl.parse(ParserImpl.java:117)
    at Main.process(Main.java:299)
    at Main.main(Main.java:113)

The error message clearly indicates that the parser is not familiar with TypeScript and does not have support for it. There seems to be no parameter available to specify the language variant.

Am I missing something here? Is there a workaround to make this work?

Answer №1

Unfortunately, the Nashorn Parser API only supports ECMAScript ES5.1 and ES6 versions. I discussed this in detail in a communication on the Nashorn developer mailing list , where I made no mention of TypeScript.

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

What causes the string to be treated as an object in React Native?

I am fetching a string value through an API and I need to display it in the View. However, the string value is coming as a Promise. How can I handle this? "Invariant Violation: Objects are not valid as a React child (found: object with keys {_40, _65 ...

Invoking servlet using Ajax

I have created a JSP file with Javascript functions and AJAX calls to invoke a servlet (ReadprojectInfo). <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE ...

Refreshing data in Laravel after an AJAX request

Currently, he is working on coding that involves filtering data within a table. Using Ajax to call the link will retrieve a response (json) with the answers. However, there has been an issue. The task at hand is to render tables without using append or sim ...

Discover the steps to convert an image to base64 while circumventing the restrictions of the same-origin policy

I've been struggling to convert an image link to base64 in order to store it on the client-side browser (IndexedDB). Despite searching for a solution for days, I have not been able to find one that addresses my issue. While I can successfully convert ...

Tips for stopping a file from being downloaded while printing using the print-html-element npm library?

Working on a desktop app using Electron Node.js, I have integrated the print-html-element library to print HTML pages. The printing process works fine, but after selecting the printer (Microsoft XPS Document Writer), it prompts me to download the output as ...

Utilizing Titanium MVC: Invoke function and pause for response

Currently, I am in the process of developing my very first Titanium iPhone application. Within a model module, I have this code snippet: (function() { main.model = {}; main.model.getAlbums = function(_args) { var loader = Titanium.Ne ...

Issue with ReactJS and Material UI: FlexGrow functionality malfunctioning

I'm currently grappling with the implementation of FlexBox, particularly in understanding how to effectively utilize flexGrow. Despite my efforts, I haven't been able to achieve the desired result as the background appears to only wrap around the ...

The "VsTsc" operation was unable to start due to issues with its provided input parameters

Encountering the following error when building an Asp.NetCore project in Visual Studio Enterprise 2017 Version 15.6.0. The error sometimes disappears upon restarting Visual Studio, but a permanent solution has not been found. Error MSB4064: The "Compu ...

Access-Control-Allow-Origin does not permit the origin null

I've been searching for an answer to this question, but I haven't found a suitable one yet. let xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(){ if(xhr.readyState === 4 && xhr.status === 200){ let response = r ...

invoking a function through prototype

<script> var Nancy = function(){ this.name = 'nancy' } Nancy.prototype.getNancy = function(){ alert(this.name); } Nancy.prototype.getNancy(); function Bob(){ ...

Utilizing WebFilter, Expression Language (EL), and SessionScoped ManagedBean for an efficient Remember Me login

Recently, I posted a question related to understanding HttpServletRequest and cookies in JSF on Stack Overflow. In my effort to implement a "Remember Me" login feature in JSF, I am using a cookie that is read in a WebFilter. The filter successfully retrie ...

Guide on passing a shortened object to the view in Express.js

Hey there, I'm new to programming and currently working on setting up a basic blog using Express.js and Mongoose. In my code snippet below, I have successfully written a function that displays 6 articles from my database (each article has a simple Art ...

Using Java to send an HTTP POST request with JSON data

Attempting to connect to an API to retrieve information in JSON format. The request needs to be made with JSON encoded authorization details using the POST method, but encountering errors. The goal is to receive the response in a JSON object. Using apache ...

Mongoose fails to save due to an error stating "undefined id"

Having some trouble with the Mongoose save function... In my user model file: const mongoose = require('mongoose'); const Schema = mongoose.Schema; const User = mongoose.model('User', { name: Schema.Types.Mixed, gender: String, ...

Adjust the value of a select box to the chosen option using JQuery

Could someone please assist me with this code? I am trying to adapt it for use on my mobile device with jQuery Mobile. var obj = jQuery.parseJSON(finalresult); //alert(obj); var dept = '2'; $(document).ready(function () { //$("#opsContactId ...

The concept of a singleton design pattern is like a hidden treasure waiting to be

My approach to implementing the singleton pattern in a typescript ( version 2.1.6 ) class is as follows: export class NotificationsViewModel { private _myService: NotificationService; private _myArray: []; private static _instance: Notificatio ...

Extract HTML content using CKEditor

Hey there! I'm in need of some help with getting user-entered data from a textarea. I've already attempted using CKEDITOR.instances.editor1.getData() and CKEDITOR.instances.ckeditor.document.getBody.getHtml(), but unfortunately both only return ...

Strategies for resolving the TypeScript error code TS7009?

I am currently new to Typescript programming and in the learning phase. I encountered a problem while coding and received an error in the backend console. Here is the code snippet that caused the error: function employee(id:number,name:string) { this.i ...

Twice Triggered: Firebase Cloud Function HTTPS Call

I have thoroughly reviewed the Firebase Cloud Functions reference, guides, and sample code in an attempt to solve the issue of my function being triggered twice, but so far, I have not found a solution. I also experimented with Firebase-Queue as a workarou ...

Is there a way to determine where a Javascript event originated from when it was triggered programmatically?

In my current debugging situation, I am investigating why pressing Enter on a submit button triggers a 'click' event on that same button. It appears that the click event is being fired programmatically, which is the expected behavior in the appli ...