JDK 9中Nashorn解析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.tsfiles into standard JavaScript, then pass the compiled.jsfiles to Nashorn. You can automate this in Java usingProcessBuilderto runtscprogrammatically, 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 liketypescript-javawrap 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 itsgraalvm-typescriptcomponent. 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

