[type] 类型的参数不可分配给 [type] 类型的参数
Posted
技术标签:
【中文标题】[type] 类型的参数不可分配给 [type] 类型的参数【英文标题】:Argument of type [type] is not assignable to parameter of type [type] 【发布时间】:2019-02-08 08:13:43 【问题描述】:我在运行 npx webpack
时收到此错误
Version: webpack 3.12.0
Time: 57478ms
Asset Size Chunks Chunk Names
app.js 172 kB 0 [emitted] app
vendor.js 7.91 MB 1 [emitted] [big] vendor
app.js.map 256 kB 0 [emitted] app
vendor.js.map 9.28 MB 1 [emitted] vendor
[21] ./angular/service.ts 3.92 kB 0 [built]
[23] ./angular/Utility.ts 2.21 kB 0 [built]
[37] (webpack)/buildin/global.js 509 bytes 1 [built]
[42] ./angular/component/empty.ts 1.35 kB 0 [built]
[66] ./angular/api.ts 3.15 kB 0 [built]
[85] ./angular/modules/LoadComponentModule.ts 1.92 kB 0 [built]
[250] ./angular/bootstrap.ts 2.58 kB 0 [built]
[251] ./angular/env.json 325 bytes 0 [built]
[587] ./angular/ErrorCatcher.ts 2.57 kB 0 [built]
[588] ./angular/modules/MaterialModules.ts 2.4 kB 0 [built]
[589] ./angular/modules/AFModules.ts 1.17 kB 0 [built]
[594] ./angular/Pipes.ts 5.64 kB 0 [built]
[595] ./angular/component/layout.ts 2.52 kB 0 [built]
[601] ./angular/component/management/wrapper.ts 3.91 kB 0 [built]
[605] ./angular/component/project/project_list.ts 4.87 kB 0 [built]
+ 595 hidden modules
ERROR in ./angular/component/search.ts
[tsl] ERROR in D:\nodeapps\myproject\angular\component\search.ts(101,34)
TS2345: Argument of type 'string' is not assignable to parameter of type 'CollectionReference'.
ERROR in ./angular/component/search.ts
[tsl] ERROR in D:\nodeapps\myproject\angular\component\search.ts(102,26)
TS2345: Argument of type 'CollectionReference' is not assignable to parameter of type 'Query | CollectionReference'.
Type 'firebase.firestore.CollectionReference' is not assignable to type 'FirebaseFirestore.CollectionReference'.
Property 'offset' is missing in type 'CollectionReference'.
文件中的错误行是this.collectionPath
和org
:
this.ref = this.afs.collection(this.collectionPath, org=>
var r = this.whereAll(org, this.requirementQuerys);
this.params.forEach(p=>
r = this.whereAll(r, p.querys);
);
return this.orderBy ? r.orderBy(this.orderBy) : r;
);
这是我来自 package.json
的依赖项:
"dependencies":
"firebase-admin": "^5.12.1",
"firebase-functions": "^1.0.4",
"firebase-tools": "^3.18.6"
,
"devDependencies":
"@angular/animations": "^5.2.5",
"@angular/cdk": "^5.2.1",
"@angular/cli": "^1.6.8",
"@angular/common": "^5.2.5",
"@angular/compiler": "^5.2.5",
"@angular/core": "^5.2.5",
"@angular/forms": "^5.2.5",
"@angular/http": "^5.2.5",
"@angular/material": "^5.2.1",
"@angular/platform-browser": "^5.2.5",
"@angular/platform-browser-dynamic": "^5.2.5",
"@angular/router": "^5.2.5",
"@google-cloud/functions-emulator": "^1.0.0-beta.4",
"@types/hammerjs": "^2.0.35",
"ajv": "^6.0.0",
"angularfire2": "^5.0.0-rc.6",
"concurrently": "^3.5.1",
"es6-shim": "~0.35.3",
"exports-loader": "~0.7.0",
"expose-loader": "~0.7.4",
"firebase": "^5.0.4",
"glob": "^7.1.2",
"hammerjs": "^2.0.8",
"http-server": "^0.11.1",
"imports-loader": "~0.7.1",
"node-sass": "^4.7.2",
"reflect-metadata": "~0.1.12",
"rxjs": "^5.5.6",
"ts-loader": "^3.5.0",
"typescript": "^2.7.1",
"webpack": "^3.11.0",
"webpack-dev-server": "^2.11.1",
"webpack-sources": "^1.1.0",
"zone.js": "~0.8.20"
,
这是我的webpack.config.js
require('./html_style');
const path = require('path');
const webpack = require('webpack');
const CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin;
const isVendor = function(module)return module.context && module.context.indexOf("node_modules") !== -1;;
module.exports =
devtool: 'source-map',
watch: false,
entry:
'app': './angular/bootstrap.ts'
,
output:
filename: '[name].js',
path: path.resolve('./public/angular')
,
resolve:
extensions: ['.ts', '.tsx', '.js']
,
module:
exprContextCritical: false,
loaders: [
test: /\.ts?$/, exclude: /node_modules/, loader: 'ts-loader'
]
,
plugins: [
new CommonsChunkPlugin(name: 'vendor', minChunks: isVendor)
]
我一直在谷歌搜索,但没有运气。任何想法?我的平台是Windows。我的node版本是10.9.0,npm版本是6.4.0。
【问题讨论】:
尝试断言this.collectionPath
是CollectionReference
类型。
可能会显示一些代码和相关的模型类
@David 实际上该项目来自其他开发人员。我在本地设置。我试图理解代码。我正在为第一个错误here 和here 发送一些代码截图,这可能会有所帮助。
听起来像带有两个参数的签名要求第一个不是是字符串?
【参考方案1】:
您的问题有点类似于https://github.com/angular/angularfire2/issues/1467。当您的 angularfire2 和 firebase 库版本不匹配时,就会发生这种情况。您的问题的解决方案是使用强类型。我已经修改了你的代码,所以你会有一个如何解决你的问题的例子:
import AngularFirestore, Query, QueryFn from 'angularfire2/firestore';
let query: QueryFn = org =>
let r: Query = this.whereAll(org, this.requirementQuerys);
this.params.forEach(p =>
r = this.whereAll(r, p.querys);
);
return this.orderBy ? r.orderBy(this.orderBy) : r;
;
this.ref = this.afs.collection(this.collectionPath, query);
以防万一您仍有问题,我使用了"angularfire2": "^5.0.0-rc.12"
和"firebase": "^5.4.0"
。将来查看 angularfire2 github 问题以解释如果在下一次更新依赖项后您的项目不起作用,哪些版本是兼容的。
【讨论】:
导入行是怎么回事?我已经有these line of import。您建议的导入会导致其他地方出现错误。 请查看我的其他评论***.com/questions/52146905/… 尝试将谷歌云导入更改为 angularfire 导入,因为您混淆了来自不同库的导入,所以您遇到了问题【参考方案2】:认为这一行暗示了主要问题:
Type 'firebase.firestore.CollectionReference' is not assignable to type 'FirebaseFirestore.CollectionReference'.
显然有两个不同的 CollectionReference 类。一个位于 firbase.firestore,另一个位于 FirebaseFireStore。那么可能你有错误的导入?
【讨论】:
不确定这是答案还是评论。请问可以展开吗?您正在谈论第二个错误。第一个错误不是那个。 我想说的是,您的项目中有两个不同的 CollectionReference 类。在没有看到更多代码的情况下,我无法准确判断出了什么问题,但它有助于查看您的导入部分和 whereAll 函数。见github.com/angular/angularfire2/…。而github.com/firebase/firebase-js-sdk/… FirebaseFirestore 似乎直接来自 Firebase 库,而不是来自 angular2fire 库 事实上该项目来自其他开发者。我在本地设置。我试图理解代码。我正在为第一个错误here 和here 发送一些代码截图,这可能会有所帮助。以上是关于[type] 类型的参数不可分配给 [type] 类型的参数的主要内容,如果未能解决你的问题,请参考以下文章
参数类型“Type”不能分配给参数类型“FirebaseUser”
参数类型 'num?'不能分配给参数类型 'num'.dart(argument_type_not_assignable)