The target field
The **lib**
property lets us specify what version of JavaScript our source files can use; the **target**
property lets us say what version of JavaScript our output files use. This is useful when we want to use newer features of JavaScript while still supporting older browsers and environments.
The default setting for **target**
is "ES3", which means it supports all the way back to Internet Explorer 8! These days, it's rare for anyone to be using a browser that doesn't support at least ES2015, so we can usually change the **target**
field to **ES2015**
. Supporting more recent JavaScript versions, like **ES2019**
, might decrease the size of your bundle and make your program more efficient, since you don't have to include large polyfills and JavaScript engines can optimize newer syntax to make it faster than older syntax. Ultimately, the choice is up to you and what browser versions your users have.
In short, pick a **lib**
value that covers what code you'll be writing, and pick a **target**
value that covers your code output. You want your **target**
to be as recent as possible, while still supporting all the people that need to run your code.
Just for fun, let's see how a simple piece of TypeScript code is compiled into different **target**
s.
Source:
ES5 (20 lines longer):
ES5 doesn't have class support, so it has to make the necessary adjustments to the prototype of FruitBasket to make it behave like a class.
To get private fields working, ES5 has to add a special polyfill which uses a WeakMap to keep track of the fruit list. That adds a considerable number of lines to the output, and isn't as performant as proper private fields could be.
Since this polyfill requires WeakMap support, this polyfill isn't even compatible with ES5. We would have to use TypeScript **private**
fields instead.
ES2015 (18 lines longer):
We are able to shave off a few lines of implementation by using ES2015 classes instead of prototypes. Private fields are still not supported, so we need that lengthy polyfill at the top.
ESNext (6 lines longer)
A future version of JavaScript will ship with proper private field support, which will dramatically reduce the implementation size by getting rid of the polyfill. Without the polyfill, we won't need to use that WeakMap hack, which should provide more security and more opportunity for browser efficiency improvement.