Tslint: Keine ungenutzte Variable für Typalias und Schnittstelle bei Verwendung als Generics.

Erstellt am 2. Apr. 2017  ·  24Kommentare  ·  Quelle: palantir/tslint

Fehlerbericht

  • __TSLint-Version__: 5.0.0
  • __TypeScript-Version__: 2.2.2
  • __TSLint ausführen über__: CLI

TypeScript-Code wird linted

describe('Action', () => {
    describe('actionCreator', () => {
        type DummyActionType = 'FOO' | 'BAR';

        interface Point {
            readonly x: number;
            readonly y: number;
        }

        it('creates the correct FSA for primitives', () => {
            const creator = actionCreator<DummyActionType, number>('FOO');
            creator(7).should.deep.equal({ type: 'FOO', payload: 7 });
        });

        it('creates the correct FSA for objects', () => {
            const creator = actionCreator<DummyActionType, Point>('BAR');
            creator({ x: 1, y: 3 }).should.deep.equal({
                type: 'BAR',
                payload: { x: 1, y: 3 },
            });
        });
    });
});

mit tslint.json Konfiguration:

{
    "defaultSeverity": "error",
    "extends": [
        // "tslint-microsoft-contrib",
        "tslint:recommended",
        "tslint-react"
    ],

    "rulesDirectory": [
        "./node_modules/tslint-immutable/rules"
    ],

    // additional rules aside from inherited ones
    "rules": {
        "arrow-parens": [true, "ban-single-arg-parens"],

        // possible errors (core TSLint)
        "no-switch-case-fall-through": true,
        "no-string-throw": true,

        // stylistic (core TSLint)
        "array-type": [true, "array"],
        "interface-name": [true, "never-prefix"],
        "no-null-keyword": true,
        "no-require-imports": true,
        "object-literal-sort-keys": false,
        "ordered-imports": [false], // array makes VSCode happy
        "quotemark": [true, "single", "jsx-double", "avoid-escape"],

        // best practices (core TSLint)
        "linebreak-style": [true, "LF"],
        "max-file-line-count": [true, 300],
        "max-line-length": [true, 100],
        "no-magic-numbers": true,
        "no-unused-expression": true,
        "no-unused-variable": [true, "check-parameters", "react"],
        "one-line": [false],
        "prefer-const": true,

        // functional programming
        "no-let": true, // no-var on by default
        "no-this": true,
        "no-class": true,
        "no-new": false, // records?
        "readonly-interface": true,
        "readonly-indexer": true,
        "readonly-array": true,
        "no-mixed-interface": true,

        // react
        "jsx-no-multiline-js": false,

        // legal
        "file-header": [true, "[+ ]{10,}"]
    }
}

Tatsächliches Verhalten

Es markiert den Typalias und die Schnittstelle als unbenutzt.

src/frontend/actions/Action.test.ts
'DummyActionType' is declared but never used. (no-unused-variable)
  15 | describe('Action', () => {
  16 |     describe('actionCreator', () => {
> 17 |         type DummyActionType = 'FOO' | 'BAR';
     |             ^
  18 | 
  19 |         interface Point {
  20 |             readonly x: number;

'Point' is declared but never used. (no-unused-variable)
  17 |         type DummyActionType = 'FOO' | 'BAR';
  18 | 
> 19 |         interface Point {
     |                  ^
  20 |             readonly x: number;
  21 |             readonly y: number;
  22 |         }

Erwartetes Verhalten

Der Typalias und die Schnittstelle werden als generische Argumente verwendet und sollten keine Lint-Fehler aufweisen.

P1 Requires Type Checker Fixed Bug

Hilfreichster Kommentar

Das erleben wir auch noch. Hier ist ein minimales Repo (das kleinste, das ich finden kann):

box.ts:

export class Box<T> {
  value: T;
}

box_holder.ts:

import { Box } from './box';

export class BoxHolder<T> {
  box: Box<T>;
}

Ergebnis:

ERROR: src/base/tests/box_holder.ts[3, 24]: 'T' is declared but never used.

Versionen:

  • tslint: 5.1.0
  • Typoskript: 2.3.0

Alle 24 Kommentare

Sieht aus wie Microsoft/TypeScript#14953

Mein Code führt eine Typprüfung durch, es scheint also kein Typescript-Fehler zu sein. Als "Problemumgehung" habe ich "noUnusedLocals" und "noUnusedParameters" in tsconfig aktiviert und no-unused-variable deaktiviert.

In dieser Datei

import {buildModel} from '../src/compile/common';
import {FacetModel} from '../src/compile/facet';
import {LayerModel} from '../src/compile/layer';
import {Model} from '../src/compile/model';
import {UnitModel} from '../src/compile/unit';
import {initConfig} from '../src/config';
import {ExtendedSpec, FacetSpec, LayerSpec, normalize, TopLevel, UnitSpec} from '../src/spec';

export function parseModel(inputSpec: TopLevel<ExtendedSpec>): Model {
  const spec = normalize(inputSpec);
  return buildModel(spec, null, '', initConfig(inputSpec.config));
}

export function parseUnitModel(spec: TopLevel<UnitSpec>) {
  return new UnitModel(spec, null, '', initConfig(spec.config));
}

export function parseLayerModel(spec: TopLevel<LayerSpec>) {
  return new LayerModel(spec, null, '', initConfig(spec.config));
}

export function parseFacetModel(spec: TopLevel<FacetSpec>) {
  return new FacetModel(spec, null, '', initConfig(spec.config));
}

Ich bekomme diese Fehler:

ERROR: test/util.ts[7, 9]: 'ExtendedSpec' is declared but never used.
ERROR: test/util.ts[7, 23]: 'FacetSpec' is declared but never used.
ERROR: test/util.ts[7, 34]: 'LayerSpec' is declared but never used.
ERROR: test/util.ts[7, 66]: 'UnitSpec' is declared but never used.

Es sieht so aus, als ob tslint denkt, dass Variablen in Generika nicht verwendet werden.

Das ist ziemlich ärgerlich, da ich die Importe nicht entfernen kann. Im Moment muss ich die Option deaktivieren, um nach unbenutzten Variablen zu suchen.

Ich glaube nicht, dass dies https://github.com/Microsoft/TypeScript/issues/14953 ist. @joscha hat in https://github.com/palantir/tslint/issues/2621 ein sauberes Repro erstellt, in dem die Importe nicht unterbrochen werden (https://gist.github.com/joscha/6633bae73fb4b143cfb685b2754259c9). Stoßende Priorität auf diesem.

In diesem Fall gibt es noch den Import von React (nicht abgebildet). Wenn Sie stattdessen declare namespace React { class Component<T, U> {} } verwenden, verschwindet der Fehler.

Ich habe dieses Problem immer noch und no-unused-variable ist in tslint 5 immer noch unbrauchbar. Ich helfe gerne mit Beispielen.

Das erleben wir auch noch. Hier ist ein minimales Repo (das kleinste, das ich finden kann):

box.ts:

export class Box<T> {
  value: T;
}

box_holder.ts:

import { Box } from './box';

export class BoxHolder<T> {
  box: Box<T>;
}

Ergebnis:

ERROR: src/base/tests/box_holder.ts[3, 24]: 'T' is declared but never used.

Versionen:

  • tslint: 5.1.0
  • Typoskript: 2.3.0

Interessanterweise scheint der Name des importierenden Moduls von Bedeutung zu sein. Nehmen wir das obige Beispiel mit box_holder.ts , das aus box.ts box_holder.ts importiert und box_holder.ts in ein paar andere Namen umbenannt wird, ergibt:

bo.ts : Fehler
bo_.ts : Fehler
bow.ts : Fehler
box_.ts : Fehler
box_holder.ts : Fehler
boxa.ts : kein Fehler
boy.ts : kein Fehler
bx.ts : kein Fehler

Kam dafür hierher. Ich erhalte eine ganze Reihe neuer Lint-Fehler wie folgt:

ERROR: src/app/app-state.ts[1, 1]: All imports are unused.
ERROR: src/app/app.component.ts[2, 15]: 'Platform' is declared but never used.
ERROR: src/auth/auth.service.ts[2, 10]: 'Http' is declared but never used.

All dies werden _nicht_ als Variablen verwendet, sondern eher als _type_-Definitionen.

import { AuthState } from '../auth/models/auth-state'; // <-- "All imports are unused"

export declare interface AppState {
  auth: AuthState;
}

Dies sollte jetzt in typescript@next behoben sein. Könntet ihr es alle testen?

Immer noch das gleiche Problem bei [email protected]
Verwenden von [email protected]

import * as TypeMoq from 'typemoq';
import { MockedMethod } from '../mocking';
import { IComponent } from '../component';  // <-- All imports are unused
import { IComponentFactory } from '../componentfactory';

export class CreateComponentXMethodMock extends MockedMethod<IComponentFactory, IComponent> {
    constructor(mock: TypeMoq.IMock<IComponentFactory>) {
        super(mock, x => x.createComponentX(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAnyNumber()));
    }
}

Okay, Problem nochmal geöffnet. https://github.com/Microsoft/TypeScript/issues/14953#issuecomment -302101264

behoben in TS 2.4

Ich sehe dies ohne einen Fehler in tsc :

interface SurveyAssetLayersProxyProps {
  survey: Survey
}

export default class SurveyAssetLayersControl extends React.Component<SurveyAssetLayersProxyProps, void> {
}

Gibt mir: ERROR: 19:11 no-unused-variable 'SurveyAssetLayersProxyProps' is declared but never used.

@mikew Vielleicht tslint mit einer anderen TypeScript-Version als Ihr tsc ?

Mir war nicht bewusst, dass das möglich ist. Beide sind relativ zum Projekt, in ./node_modules/.bin/ . Wie würde ich das bestätigen?

Am 2. Juni 2017 um 14:52 Uhr schrieb Andy [email protected] :

@mikew https://github.com/mikew Vielleicht läuft Ihr Tslint mit einer anderen TypeScript-Version als Ihr Tsc?


Sie erhalten dies, weil Sie erwähnt wurden.
Antworten Sie direkt auf diese E-Mail, zeigen Sie sie auf GitHub an https://github.com/palantir/tslint/issues/2470#issuecomment-305864709 , oder schalten Sie den Thread stumm https://github.com/notifications/unsubscribe-auth/AAASeRvhBbEUpy0tQwk_wtObSYT9bsndjspZAv4 .

Ein anderes tsc in tslint ist nicht wirklich möglich... @mikew welche genaue Version von 2.4 nightly verwendest du?

@adidahiya im Moment funktioniert es nicht, weil ihr die Deps nur in devDependencies aktualisiert, aber nicht in peerDependencies . siehe hier https://github.com/palantir/tslint/blob/3323ed2b0824a12c8b35c421ebf23c7d17cf788f/package.json#L51. Ich hoffe, Patch-Release so schnell wie möglich zu erhalten :smile:

Ooh, ich habe versucht, Tslint zu entfernen und neu zu installieren. Und sein verschachteltes Typoskript ist weg. Mit Garn stimmt was nicht.

Ich habe dieses Problem mit v5.6.0

@tolgaek Könnten Sie Beispielcode bereitstellen? Was ist deine TS-Version? Können Sie Ihren Fehler mit der Compileroption --noUnusedLocals anstelle von tslint reproduzieren?

Hallo @andy-ms Ich habe versucht, dieses Problem mit einfachem Code zu reproduzieren, aber der Fehler wurde dann nicht angezeigt. Es sieht so aus, als ob dieser Fehler auf die Komplexität unserer App zurückzuführen sein könnte.

Ich konnte auch nicht mit noUnusedLocals laufen, aber ich erhalte den Fehler "Fehler: unbekannte Option `--noUnusedLocals'".

Hier ist der Code, bei dem der Fehler angezeigt wird:

import { StepUpProvider } from './step-up.provider';
import { Injectable } from '@angular/core';
import {
  Http,
  XHRBackend,
  RequestOptions,
  RequestOptionsArgs,
  Response,
  Request
} from '@angular/http';
import { Observable } from 'rxjs';

@Injectable()
export class RequestInterceptor extends Http {

  constructor(
    public backend: XHRBackend,
    public defaultOptions: RequestOptions,
    private stepUpProvider: StepUpProvider
  ) {
    super(backend, defaultOptions);
  }

  request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
    return this.stepUpProvider.intercept(super.request.bind(this, url, options));
  }
}

In der obigen Datei erhalte ich **.ts[8, 3]: 'Response' is declared but never used. obwohl Response wird, um den Rückgabewert der Funktion request einzugeben

Ich sehe dies mit tslint 5.10.0 und typescript 2.9.2. Irgendwelche Ideen?

🤖 Beep boop! 👉 TSLint ist veraltet 👈 und Sie sollten zu typescript-eslint wechseln ! 🤖

🔒 Dieses Problem wird gesperrt, um weitere unnötige Diskussionen zu vermeiden. Dankeschön! 👋

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen