Enforce stricter types via lint when in strict mode

🚀 Feature request

Command

  • generate library

Description

By default, it is not required to explicitly type functions’ return value, as TypeScript can always infer the type based on the function code.

But the inference assumes that the function’s code is correct. If the code change or is incorrect, the return type inference will reflect that.

While it is acceptable in applications, as it will raise an error where the function is used if the types do not match anymore, it is more dangerous in a library, as the functions are not used directly. It can lead to unwanted breaking changes in the public API.

Describe the solution you’d like

TSLint has a rule to enforce functions’ return type (and ESLint too, for the future migration):

{
  "rules": {
    "typedef": [true, "call-signature"]
  }
}

It could be added when doing ng g library.

It is an easy change as:

  • it won’t impact applications: libraries already have a specific tslint.json due to --prefix option
  • it won’t impact already created libraries as it only happens when generating a new one

This is a really easy thing to add, I can take care of the PR if Angular team signal is green.

If it is considered a too big step, it could be added only if the workspace is already using TypeScript strict mode (following the new --strict option in Angular >= 9). But I think as we are talking about libraries, it should really be the default: libraries authors must be stricter to avoid compatibility issues (strict mode should even be the default for libraries, but it would be another discussion).

1 possible answer(s) on “Enforce stricter types via lint when in strict mode

  1. @cyrilletuzi, the end goal is indeed to make strict mode the default in the future, but we cannot yet. Because we want to make sure that all the courses and docs out there are complaint with strict mode, as don’t want to invalid courses, nor make it harder for new developers to use Angular just because some explains are not strict complaint.

    I agree that when not using TypeScript in strict mode, you will use only a very small fraction of the potential of the TS type-checker which will lead to bugs which could be caught at an earlier stage.

    Thanks for your PR bdw 😁