Tslint: 当用作泛型时,类型别名和接口没有未使用的变量。

创建于 2017-04-02  ·  24评论  ·  资料来源: palantir/tslint

错误报告

  • __TSLint 版本__:5.0.0
  • __打字稿版本__:2.2.2
  • __通过__运行TSLint:CLI

正在整理 TypeScript 代码

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 },
            });
        });
    });
});

使用tslint.json配置:

{
    "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,}"]
    }
}

实际行为

它将类型别名和接口标记为未使用。

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 |         }

预期行为

类型别名和接口用作通用参数,并且应该没有 lint 错误。

P1 Requires Type Checker Fixed Bug

最有用的评论

我们也在经历这种情况。 这是一个最小的回购(我能找到的最小的):

box.ts:

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

box_holder.ts:

import { Box } from './box';

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

结果:

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

版本:

  • tslint:5.1.0
  • 打字稿:2.3.0

所有24条评论

看起来像微软/TypeScript#14953

我的代码会检查类型,所以它似乎不是 Typescript 错误。 作为“解决方法”,我在 tsconfig 中打开了"noUnusedLocals""noUnusedParameters" ,并关闭了no-unused-variable

在这个文件中

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));
}

我收到这些错误:

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.

看起来 tslint 认为没有使用泛型中的变量。

这很烦人,因为我无法删除导入。 现在我必须禁用检查未使用变量的选项。

我不认为这是https://github.com/Microsoft/TypeScript/issues/14953。 @joschahttps://github.com/palantir/tslint/issues/2621中生成了一个干净的repro ,其中导入没有中断(https://gist.github.com/joscha/6633bae73fb4b143cfb685b2754259c9)。 优先考虑这一点。

在这种情况下,仍然存在React的导入(未显示)。 如果您改用declare namespace React { class Component<T, U> {} } ,错误就会消失。

我仍然遇到这个问题,并且no-unused-variable在 tslint 5 中仍然无法使用。我很乐意提供示例来提供帮助。

我们也在经历这种情况。 这是一个最小的回购(我能找到的最小的):

box.ts:

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

box_holder.ts:

import { Box } from './box';

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

结果:

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

版本:

  • tslint:5.1.0
  • 打字稿:2.3.0

有趣的是,导入模块的名称似乎很重要。 以上面的例子为例, box_holder.tsbox.ts box_holder.ts导入,将box_holder.ts重命名

bo.ts : 错误
bo_.ts : 错误
bow.ts : 错误
box_.ts : 错误
box_holder.ts : 错误
boxa.ts : 没有错误
boy.ts : 没有错误
bx.ts : 没有错误

为此而来。 我收到了一大堆新的 lint 错误:

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.

所有这些都 _not_ 被用作变量,而是 _type_ 定义。

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

export declare interface AppState {
  auth: AuthState;
}

现在应该在typescript@next修复此问题。 大家可以测试一下吗?

仍然在[email protected]上遇到同样的问题
使用[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()));
    }
}

好的,重新打开问题。 https://github.com/Microsoft/TypeScript/issues/14953#issuecomment -302101264

在 TS 2.4 中修复

我在tsc看到这个没有错误:

interface SurveyAssetLayersProxyProps {
  survey: Survey
}

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

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

@mikew也许您的tslint运行的 TypeScript 版本与tsc

我不知道这是可能的。 两者都是相对于项目的,在./node_modules/.bin/ 。 我将如何确认这一点?

2017 年 6 月 2 日下午 2:52,Andy [email protected]写道:

@mikew https://github.com/mikew也许您的 tslint 运行的 TypeScript 版本与您的 tsc 不同?


你收到这个是因为你被提到了。
直接回复本邮件,在 GitHub 上查看https://github.com/palantir/tslint/issues/2470#issuecomment-305864709 ,或将线程设为静音https://github.com/notifications/unsubscribe-auth/AAASeRvhBbEUpy0tQwk_wtObSYT9bsdMcgamts5

tslint使用不同的tsc是不可能的... @mikew您每晚使用哪个确切版本的 2.4?

@adidahiya目前,它不起作用,因为你们只升级devDependencies的 deps 而不是peerDependencies 。 见这里https://github.com/palantir/tslint/blob/3323ed2b0824a12c8b35c421ebf23c7d17cf788f/package.json#L51。 希望尽快发布补丁 :smile:

哦,我试图删除并重新安装 tslint。 它的嵌套打字稿消失了。 纱线有些不对劲。

我在 v5.6.0 中遇到了这个问题

@tolgaek你能提供示例代码吗? 你的TS版本是什么? 您可以使用--noUnusedLocals编译器选项而不是 tslint 来重现您的错误吗?

嗨@andy-ms 我试图用简单的代码重新创建这个问题,但错误不会出现。 看起来这个错误可能是由于我们的应用程序的复杂性造成的。

此外,我无法使用noUnusedLocals运行,但出现“错误:未知选项`--noUnusedLocals'”错误。

这是我看到错误的代码:

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));
  }
}

在上面的文件中,我得到**.ts[8, 3]: 'Response' is declared but never used.即使Response用于键入request函数的返回值

我在 tslint 5.10.0 和 typescript 2.9.2 中看到了这一点。 有任何想法吗?

🤖 哔哔! 👉 TSLint 已弃用👈,您应该切换到typescript-eslint ! 🤖

🔒 此问题已被锁定,以防止进一步不必要的讨论。 谢谢! 👋

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

dashmug picture dashmug  ·  3评论

allbto picture allbto  ·  3评论

DanielKucal picture DanielKucal  ·  3评论

cateyes99 picture cateyes99  ·  3评论

CSchulz picture CSchulz  ·  3评论