有没有办法让Typescript抛出错误,而不是用“any”替换类型?
在很多情况下,我被一些我想禁用的打字脚本行为烧坏了 我最近意识到我正在使用的一个库的Promise类型对有没有办法让Typescript抛出错误,而不是用“any”替换类型?,typescript,Typescript,在很多情况下,我被一些我想禁用的打字脚本行为烧坏了 我最近意识到我正在使用的一个库的Promise类型对@types/bluebird具有对等依赖性 我没有安装这个依赖项。这样做的结果是,该库中任何返回bluebirdPromise的方法,在我的代码中推断的返回类型都是any。例如: // Library .d.ts file import * as Bluebird from 'bluebird'; const Promise: typeof Bluebird; export const li
@types/bluebird
具有对等依赖性
我没有安装这个依赖项。这样做的结果是,该库中任何返回bluebirdPromise
的方法,在我的代码中推断的返回类型都是any
。例如:
// Library .d.ts file
import * as Bluebird from 'bluebird';
const Promise: typeof Bluebird;
export const libraryFunction = () => Promise.resolve(42);
// My code .ts file
// Should be () => Promise<number>
// is actually () => any
const yUNoWorkRight = async () => libraryFunction();
和TSLint
{
"linterOptions": {
"exclude": [
"**/*.json"
]
},
"rules": {
"adjacent-overload-signatures": true,
"prefer-for-of": true,
"unified-signatures": true,
"no-any": true,
"label-position": true,
"no-arg": true,
"no-construct": true,
"no-invalid-this": true,
"no-misused-new": true,
"no-shadowed-variable": true,
"no-string-throw": true,
"no-var-keyword": true,
"strict-boolean-expressions": [
true,
"ignore-rhs"
],
"triple-equals": [
true,
"allow-null-check",
"allow-undefined-check"
]
},
"jsRules": {
"object-literal-sort-keys": false,
"no-console": false,
"quotemark": false,
"trailing-comma": false,
"no-empty": [
true,
"allow-empty-functions"
]
}
}
您是否在
tsconfig.json
中尝试了“strict”:true
?有效问题。答案是肯定的。我将配置详细信息添加到help.AFAICT,这是因为您启用了allowJs
。从JS文件导入的内容将是any
,因为没有类型。相关:@cartant这些导入都不是来自JS文件。尽管如此,我还是移除了那个财产,因为它不再需要了。这并没有解决handBluebird是JS的问题,当然,这就是any
的起源——除非安装了@types
。你能把问题说清楚一点吗?“替换一种类型”是什么意思?我没有看到任何类型的替换。代码段声明了一个未赋值的Promise变量,而不是类型。
{
"linterOptions": {
"exclude": [
"**/*.json"
]
},
"rules": {
"adjacent-overload-signatures": true,
"prefer-for-of": true,
"unified-signatures": true,
"no-any": true,
"label-position": true,
"no-arg": true,
"no-construct": true,
"no-invalid-this": true,
"no-misused-new": true,
"no-shadowed-variable": true,
"no-string-throw": true,
"no-var-keyword": true,
"strict-boolean-expressions": [
true,
"ignore-rhs"
],
"triple-equals": [
true,
"allow-null-check",
"allow-undefined-check"
]
},
"jsRules": {
"object-literal-sort-keys": false,
"no-console": false,
"quotemark": false,
"trailing-comma": false,
"no-empty": [
true,
"allow-empty-functions"
]
}
}