Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Struggling with the -t --target option of the tsc transpiler

 
hangman
Posts: 220
Angular Framework Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't seem to get the -t / --target option to work. I have tried
 --target 'ES2018'  
 --target ES2018
 --target "ES2018"
  -t 'ES2018'
  -t "ES2018'"
  -t ES2018
and with other values like ES2016.
Each time tsc seems to do nothing other than put the contents of the .ts file into the .js file - without even an error message



Can someone explain what I am doing wrong?  
Thank you.
 
Saloon Keeper
Posts: 2940
373
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bob Nedwor wrote:Each time tsc seems to do nothing other than put the contents of the .ts file into the .js file


The language features that you are using with TypeScript may translate directly to ES2018.

Can you show us the contents of the .ts source file and the .js transpiled file?
 
Ron McLeod
Saloon Keeper
Posts: 2940
373
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like your code is already valid ES2018 code ...


TypeScript
ES2015 (ES6), ES2018
ES3, ES5
 
Ron McLeod
Saloon Keeper
Posts: 2940
373
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use TypeScript features which don't translate to JavaScript features , then you will see a difference.  For example, a type alias:

TypeScript
ES2018
 
Bob Nedwor
hangman
Posts: 220
Angular Framework Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, everyone but I thought that the idea of tcs is to transpile TypeScript into Javascript. It works great without the --target option.
The default is ES3.

Try
  tsc --all
to see all the options
-t VERSION, --target VERSION                       Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'.

Here is my .tc file ( other than the empty lines, the same as the output .js file when I use the -- target):


Can anyone get it to work with any of the VERSION values? other than the default?
 
Ron McLeod
Saloon Keeper
Posts: 2940
373
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bob Nedwor wrote:Thanks, everyone but I thought that the idea of tcs is to transpile TypeScript into Javascript. It works great without the --target option.
The default is ES3.


I think the point you are missing is that the transpiler is generating JavaScript code.  The language features that you are using in your TypeScript code are identical in ES2018, so other than removing blank lines and maybe comments, you will not see any difference.

In my last post I tried to show that when you use something in TypeScript where there is no translatable concept it may just get dropped.  Other features like TypeScript enums for example, will get polyfilled with something functionally equivalent.  For example:

TypeScript
ES2018
 
Bob Nedwor
hangman
Posts: 220
Angular Framework Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks.
I think I see what you are saying.

So in some cases, the --target simply works as more of a pre-step?
 
Ron McLeod
Saloon Keeper
Posts: 2940
373
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
--target specifies the version of JavaScript which should be generated by tsc.

If your TypeScript code only includes features which are also included on the version that you are transpiling to, then the generated JavaScript code will basically be identical to the original TypeScript code.  The big advantage in that case would be that if you included type declarations in your TypeScript code, the transpiler can verify these at compile/transpile time.

 
Bob Nedwor
hangman
Posts: 220
Angular Framework Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for explaining.
I think I need more practice with Typescript and I should purchase Vlad Riscutia's book, Programming with Types!

So "let" is a valid key word in ES2018?
 
Ron McLeod
Saloon Keeper
Posts: 2940
373
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

Bob Nedwor wrote:So "let" is a valid key word in ES2018?


Yup - it was included starting with ES6/ES2015.  There is a good explanation in this article: Variables and Constants in ES6.

The specification for ES2018 is detailed in ECMAScript® 2018 Language Specification

 
Yeah, but is it art? What do you think tiny ad?
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!