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 安装。
只是提醒一下 - .d.ts 文件包含在 moment 中 - 无需从 @types 安装。
谢谢,这将是我的第一个问题。
tslint:all
启用no-unused-variable
。 已知此规则会干扰其他类型检查规则:#2736
您可以尝试禁用no-unused-variable
并查看错误是否消失。
您可以尝试禁用 no-unused-variable 并查看错误是否消失。
那奏效了。 但是如果我也想使用no-unused-variable
呢? 为什么这似乎只影响我的项目中的时刻 - 我有许多其他 3rd 方库可以很好地导入。
但是如果我也想使用 no-unused-variable 呢?
有几种选择:
--noUnusedLocals
和--noUnusedParameters
no-unused-variable
,第二次只运行no-unused-variable
为什么这似乎只影响我的项目中的时刻 - 我有许多其他 3rd 方库可以很好地导入。
没有人真正知道出了什么问题以及为什么会出错。 该规则以某种方式与打字稿的内部结构混乱。
最有用的评论
有几种选择:
--noUnusedLocals
和--noUnusedParameters
no-unused-variable
,第二次只运行no-unused-variable
没有人真正知道出了什么问题以及为什么会出错。 该规则以某种方式与打字稿的内部结构混乱。