Here's my TypeScript code:
// Enum
enum Direction {
North = 1,
East,
West,
South
}
console.log(Direction.East);
// Literal types
function yesOrNoQuestion(ans: "yes" | "no"): void {
console.log("You said", ans);
}
yesOrNoQuestion("yes");
// Tuple
type rgb = [red: number, green: number, blue: number];
const color: rgb = [10, 255, 255];
console.log(color);
const tuple: [boolean, string] = [true, "yes"];
console.log(tuple);
Here's the compiled code:
"use strict";
// Enum
var Direction;
(function (Direction) {
Direction[Direction["North"] = 1] = "North";
Direction[Direction["East"] = 2] = "East";
Direction[Direction["West"] = 3] = "West";
Direction[Direction["South"] = 4] = "South";
})(Direction || (Direction = {}));
console.log(Direction.East);
// Literal types
function yesOrNoQuestion(ans) {
console.log("You said", ans);
}
yesOrNoQuestion("yes");
const color = [10, 255, 255];
console.log(color);
const tuple = [true, "yes"];
console.log(tuple);
I have 3 comments in my TS code (Enum, Literal types, Tuple). But, the compiled code has only the first two comments. Is there any reason for that? TS should either keep all comments or remove all comments. Why is it only removing specific comments?
I didn't explicitly set "removeComments" in my tsconfig.json
:
{
"compilerOptions": {
"target": "ES6",
"strict": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"alwaysStrict": true
}
}
I compiled using npx tsc
command.
I expected either:
But TypeScript only preserved the first two. I didn’t set "removeComments" explicitly.
So, I am wondering:
In TS comments are always attached to some piece of code
In your case // Tuple
is attached to a compile time only code, a type definition.
This is why you don't see it in the output.