Tslint: 无法导入和使用 moment.js

创建于 2017-11-11  ·  3评论  ·  资料来源: palantir/tslint

错误报告

  • __TSLint 版本__:5.8.0
  • __打字稿版本__:2.6.1
  • __通过__运行TSLint:(选择一个)CLI

正在整理 TypeScript 代码

import * as moment from 'moment';
import { logger } from './utils/logger';

const x: moment.Moment = moment();

logger.info(x.toString());

使用tslint.json配置:

{
    "extends": ["tslint:all", "tslint-eslint-rules", "tslint-config-prettier"],
    "rules": {
        "completed-docs": [false],
        "member-ordering": [
            true,
            {
                "order": [
                    "public-static-field",
                    "protected-static-field",
                    "private-static-field",
                    "public-instance-field",
                    "private-instance-field",
                    "public-constructor",
                    "protected-constructor",
                    "private-constructor",
                    "public-static-method",
                    "protected-static-method",
                    "private-static-method",
                    "public-instance-method",
                    "private-instance-method"
                ]
            }
        ],
        "no-implicit-dependencies": false,
        "no-inferrable-types": false,
        "no-null-keyword": false,
        "prettier": [
            true,
            {
                "bracketSpacing": true,
                "jsxBracketSameLine": false,
                "parser": "typescript",
                "printWidth": 120,
                "semi": true,
                "singleQuote": true,
                "tabWidth": 4,
                "trailingComma": "all",
                "useTabs": false
            }
        ],
        "variable-name": [true, "allow-leading-underscore", "allow-pascal-case", "ban-keywords", "check-format"]
    },
    "rulesDirectory": ["tslint-plugin-prettier"]
}

实际行为

报告的错误:

/Users/mrandolph/Projects/***/***/src/server/test.ts
ERROR: 4:26   no-unsafe-any  Unsafe use of expression of type 'any'.
ERROR: 6:13   no-unsafe-any  Unsafe use of expression of type 'any'.

预期行为

使用 moment.js 的能力。 截至目前,tslint 将其检测为类型: any ,这会导致 linting 错误。 有趣的是,如果我尝试导入特定方法,我仍然会遇到相同的 no-unsafe-any 错误:

例子:

import { duration, Duration } from 'moment';
import { logger } from './utils/logger';

const x: Duration = duration(1, 'week');

logger.info(x.toString());

产生输出:

/Users/mrandolph/Projects/***/***/src/server/test.ts
ERROR: 4:21   no-unsafe-any  Unsafe use of expression of type 'any'.
ERROR: 6:13   no-unsafe-any  Unsafe use of expression of type 'any'.

只是提醒一下 - .d.ts 文件包含在 moment 中 - 无需从 @types 安装。

Duplicate

最有用的评论

但是如果我也想使用 no-unused-variable 呢?

有几种选择:

为什么这似乎只影响我的项目中的时刻 - 我有许多其他 3rd 方库可以很好地导入。

没有人真正知道出了什么问题以及为什么会出错。 该规则以某种方式与打字稿的内部结构混乱。

所有3条评论

只是提醒一下 - .d.ts 文件包含在 moment 中 - 无需从 @types 安装。

谢谢,这将是我的第一个问题。

tslint:all启用no-unused-variable 。 已知此规则会干扰其他类型检查规则:#2736
您可以尝试禁用no-unused-variable并查看错误是否消失。

您可以尝试禁用 no-unused-variable 并查看错误是否消失。

那奏效了。 但是如果我也想使用no-unused-variable呢? 为什么这似乎只影响我的项目中的时刻 - 我有许多其他 3rd 方库可以很好地导入。

但是如果我也想使用 no-unused-variable 呢?

有几种选择:

为什么这似乎只影响我的项目中的时刻 - 我有许多其他 3rd 方库可以很好地导入。

没有人真正知道出了什么问题以及为什么会出错。 该规则以某种方式与打字稿的内部结构混乱。

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

相关问题

jacob-robertson picture jacob-robertson  ·  3评论

cateyes99 picture cateyes99  ·  3评论

allbto picture allbto  ·  3评论

CSchulz picture CSchulz  ·  3评论

avanderhoorn picture avanderhoorn  ·  3评论