TypeScript differentiates between API invocations and syntax structure.
The TypeScript compiler breaks down the syntax elements (identified by special symbols like =>
, ?
, `
, #
and keywords such as class
or static
) but does not handle the API itself. To emulate API calls, like Array.prototype.flat (introduced in ES2019
), you would require an additional compiler like Babel.
Illustration
The below compiler configuration will convert the nullish coalescing operator (??
) and the class
declaration but it won't directly alter the API call to Array.prototype.flat
(as it is not supported in ES5
):
tsconfig.json
{
"compilerOptions": {
"lib": ["ES2019"],
"target": "ES5"
}
}
main.ts
export class MyConverter {
static flatten(numbers?: (number | number[])[]) {
return (numbers ?? []).flat();
}
}
main.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MyConverter = void 0;
var MyConverter = /** @class */ (function () {
function MyConverter() {
}
MyConverter.flatten = function (numbers) {
return (numbers !== null && numbers !== undefined ? numbers : []).flat();
};
return MyConverter;
}());
exports.MyConverter = MyConverter;