Skip to content

Enforce stricter types via lint when in strict mode #17821

@cyrilletuzi

Description

@cyrilletuzi

🚀 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).

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions