You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JDK 9中Nashorn解析TypeScript报错问题求助

Can Nashorn in JDK 9 parse TypeScript?

I've heard claims that Nashorn in JDK 9 should be able to parse TypeScript, but when using this code to parse the example from the TypeScript 5-minute guide:

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

The plain JavaScript example works fine, but the version with type declarations throws this 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)

Clearly the Nashorn parser doesn't support TypeScript syntax, and there's no obvious language variant parameter to enable this. Are there any workarounds I'm missing?


Great question—let’s cut to the chase: Nashorn was never designed to parse or execute TypeScript natively, even in JDK 9. The confusion might come from Nashorn adding better ES6 support in JDK 9, but TypeScript is a superset of JavaScript with its own unique syntax (like type annotations) that Nashorn’s parser doesn’t recognize.

That said, there are a few practical workarounds to handle TypeScript with Java:

  • Pre-compile TypeScript to JavaScript first
    This is the most reliable approach. Use the official TypeScript compiler (tsc) to convert your .ts files into standard JavaScript, then pass the compiled .js files to Nashorn. You can automate this in Java using ProcessBuilder to run tsc programmatically, or integrate a build step (like Maven/Gradle plugins for TypeScript) to handle compilation before your Java code runs.

  • Use a Java-based TypeScript compiler library
    Libraries like typescript-java wrap the TypeScript compiler in a Java API, letting you compile TS to JS directly within your application without spawning external processes. This keeps everything self-contained, though you’ll need to add the library as a dependency.

  • Switch to a modern JS engine with TypeScript support
    Nashorn was deprecated in JDK 11 and removed in JDK 15, so it’s not a long-term solution. Consider migrating to GraalVM JavaScript, which supports modern JS features and has built-in TypeScript support via its graalvm-typescript component. This lets you execute TypeScript directly without pre-compilation in many cases.

Unfortunately, there’s no hidden flag or parameter to make Nashorn understand TypeScript syntax—its parser is strictly built for JavaScript. The workarounds above are your best bets.


内容的提问来源于stack exchange,提问作者rwallace

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:09:25