Files
screeps-scripts/docs/in-depth/typescript.md
Resi Respati 75ee6f8b16 Finalise docs
Notes:
* Will fill in the 'What's new' section after @arcath is finished with his article draft
* Will add the legacy branch link when it's available
2017-12-02 00:57:07 +07:00

2.5 KiB

TypeScript

This section provides TypeScript-specific tips & tricks for you to make the best out of the ecosystem.

Strict mode

The --strict compiler flag was introduced in TypeScript 2.3 which activates TypeScript's "strict mode". The strict mode sets all strict typechecking options to true by default.

As of TypeScript 2.6, the affected options are:

  • --noImplicitAny
  • --noImplicitThis
  • --alwaysStrict
  • --strictNullChecks
  • --strictFunctionTypes

Starting from version 2.0 of the starter kit, we've enabled the --strict flag in tsconfig.json. If this gives you compile time errors, you can try setting "strict" to false, or by overriding one or more of the options listed above.

For more info: https://blog.mariusschulz.com/2017/06/09/typescript-2-3-the-strict-compiler-option

TSLint

TSLint checks your TypeScript code for readability, maintainability, and functionality errors, and can also enforce coding style standards.

This project provides TSLint rules through a tslint.json file, which extends the recommended rules from TSLint defined here.

We've made some changes to these rules, which we considered necessary and/or relevant to a proper Screeps project:

  • set the forin rule to false, it was forcing for ( ... in ...) loops to check if object members were not coming from the class prototype.
  • set the interface-name rule to false, in order to allow interfaces that are not prefixed with I.
  • set the no-console rule to false, in order to allow using console.
  • in the variable-name rule, added allow-leading-underscore.

Customising TSLint

You can also customise your tslint.json file to match the preferences of your codebase. Click here, to find out how, and click here for a complete list of rules available.

If you believe that some rules should not apply to a part of your code (e.g. for one-off cases like having to use require() to include a module), you can use flags to let TSLint know about it: https://palantir.github.io/tslint/usage/rule-flags/

More info about TSLint: https://palantir.github.io/tslint/