TSLint custom rule for checking the possibility of assigning function parameters (no bivariance)

I just stumbled upon the fact that TypeScript is not quite strict control over the assignment of functions: https://www.typescriptlang.org/docs/handbook/type-compatibility.html#function-parameter-bivariance

Unfortunately, for some templates, the bivariance parameter skips important type checking. So I'm wondering if it's possible to create my own TSLint rule telling me when I do something like this:

interface Base {}
interface BaseEx extends Base { x; }

let fn1: (a: Base) => void;
let fn2: (b: BaseEx) => void;

fn1 = fn2; // TSLint: parameter (a: BaseEx) is not assignable to (b: Base)

However, the documentation for creating TSLint custom rules seems rather incomplete, I found only one example of a purely syntax check. I would be very happy if you could advise me on a resource to learn how to extend TSLint with semantic rules like this.

+4
source share
1 answer

When searching for a custom rule, the TSLint source code is useful reference material. The source for all built-in rules is available in the TSLint repo . Most rules do not require access to type information. However, there are two options:

ProgramAwareRuleWalker, TypeChecker.

, TypeChecker TypeScript API.

, ProgramAwareRuleWalker, , TSLint --type-check, --project.

+4

Source: https://habr.com/ru/post/1651965/


All Articles