Benefits of using TypeScript in the SDK
Static type definitions
Static type definitions also give the code more structure, make it self-documenting, and speed up readability, debugging and refactoring. It is not a magic pill though when it comes to reducing bug density. We still need to use other quality control measures – like design review, spec review, code review, using a linter and writing tests – in addition to static types.
IDE Support (Intellisense)
Thanks to the static type definitions, the IDEs are able to provide variety of code editing features like code completion and parameter info. In one of the popular code editors Visual Studio Code, this is known as Intellisense.
In the following screenshot, Intellisense in Visual Studio Code provides details for region configuration parameter while creating DynamoDB client. It specifies that region is optional, its value can be a
string or a
Provider<string>, and it stores the AWS region to which the client will send requests.
Equivalent example in TypeScript using imports:
Latest ECMAScript features
As maintainers of the SDK, we get to use the latest and greatest features of the ECMAScript specification supported by TypeScript but still target the published artifacts to use the version supported by runtime.
At the time of writing in December 2020, the SDK is using the latest stable version of TypeScript 4.1.2 and our Node.js artifacts are targeting ES2018. Thanks to TypeScript we’re already using ES2020 features in our codebase like Nullish Coalescing and Optional chaining, and TypeScript compiles them to ES2018 for us.
For example, here is the code to select retryDecider in retry middleware which uses both optional chaining and nullish coalescing:
this.retryDecider = options?.retryDecider ?? defaultRetryDecider;
TypeScript compiles it to the following in ES2018 so that it can run in Node.js 10.x:
this.retryDecider = (_a = options === null || options === void 0 ? void 0 : options.retryDecider) !== null && _a !== void 0 ? _a : retryDecider_1.defaultRetryDecider;
When we need to drop support for Node.js 10.x in future, we can just update TypeScript configuration to target ES2019. The code doesn’t need to be updated. Similarly, when ES2021 features will get supported in future versions of TypeScript, we can start using them while still compiling code to ES2018 for supporting Node.js 10.x.
We also use TypeDoc documentation generator which is extensible and supports a variety of configurations.
We value your feedback, so please tell us what you like and don’t like by opening an issue on GitHub.