Have you ever wondered what all the different (compiler) options of tsconfig.json
are for? I know I have. In this post, I’ll zoom in on target
.
Consider the following TypeScript:
const myNumber: number = 5;
const myString: string = "word";
console.log(`${myNumber} & ${myString}`);
It uses two features implemented in later JavaScript versions:
const
to declare a variable- template variable for string interpolation (e.g.
Hello ${name}
)
If we were to target ES5, which supports neither, the JavaScript output would be as such:
var myNumber = 5;
var myString = "word";
console.log(myNumber + " & " + myString);
A modern target like ES6, supports both features. Resulting in very similar output to the TypeScript:
const myNumber = 5;
const myString = "word";
console.log(`${myNumber} & ${myString}`);
So what version should you pick? It depends on which environments you have to support. All modern browsers support ES6 so if that’s the support you need, ES6 is a good option.
Hopefully, these examples showed you what target does. To read more about target, head over to the TypeScript documentation.