这是我第一次使用打字稿,所以我决定将它与Firebase一起使用。 但是我遇到了我无法理解的错误。
我写了两个文件MessageTemplate.ts
和test.ts
运行它们之后,一切正常。
但是当我试图将它们推入(firebase部署--only函数)到firebase-functions时,出现以下错误:
> tsc
src/index.ts:18:25 - error TS2345: Argument of type '{}' is not assignable to parameter of type 'Message'.
Property 'condition' is missing in type '{}' but required in type 'ConditionMessage'.
18 admin.messaging().send(payload)
~~~~~~~
node_modules/firebase-admin/lib/index.d.ts:410:3
410 condition: string;
~~~~~~~~~
'condition' is declared here.
src/index.ts:19:11 - error TS7006: Parameter 'fcm_response' implicitly has an 'any' type.
19 .then((fcm_response) => {
~~~~~~~~~~~~
src/index.ts:24:12 - error TS7006: Parameter 'error' implicitly has an 'any' type.
24 .catch((error) => {
~~~~~
src/MessageTemplate.ts:37:5 - error TS7017: Element implicitly has an 'any' type because type '{}' has no index signature.
37 payload["notification"] = notification.toJSON()
~~~~~~~~~~~~~~~~~~~~~~~
src/MessageTemplate.ts:37:31 - error TS2532: Object is possibly 'undefined'.
37 payload["notification"] = notification.toJSON()
~~~~~~~~~~~~
src/MessageTemplate.ts:41:5 - error TS7017: Element implicitly has an 'any' type because type '{}' has no index signature.
41 payload["data"] = data
~~~~~~~~~~~~~~~
src/MessageTemplate.ts:44:3 - error TS7017: Element implicitly has an 'any' type because type '{}' has no index signature.
44 payload[type] = to
~~~~~~~~~~~~~
Found 7 errors.
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ build: `tsc`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the functions@ build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /somepath/_logs/2019-03-26T21_04_51_135Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code2
functions / src / index.ts
import { buildPushNotification, SenderType } from "./MessageTemplate";
import * as functions from 'firebase-functions';
import * as admin from'firebase-admin';
admin.initializeApp();
export const sendMessage = functions.https.onRequest((request, response) => {
// The topic name can be optionally prefixed with "/topics/".
let topic = 'global'
let data = {
id:1024,
name:"Juan"
}
let payload = buildPushNotification(SenderType.topic, topic, undefined, data)
// Send a message to devices subscribed to the provided topic.
admin.messaging().send(payload)
.then((fcm_response) => {
// Response is a message ID string.
console.log('Successfully sent message:', fcm_response);
response.send("Successfully sent message");
})
.catch((error) => {
console.log('Error sending message:', error);
response.send("Error");
});
});
MessageTemplate.ts
export enum SenderType {
token="token",
topic="topic",
condition="condition"
}
export class PNotification {
title:string
body:string
constructor(title:string, body:string) {
this.title = title
this.body = body
}
toJSON():any {
return {
"title":this.title,
"body":this.body
}
}
}
function isNullOrUndefined(data:any){
return data === null || typeof data === 'undefined'
}
export function buildPushNotification(type:SenderType, to:string, notification?:PNotification, data?:any){
let payload = {}
if(!isNullOrUndefined(notification)) {
payload["notification"] = notification.toJSON()
}
if(!isNullOrUndefined(data)) {
payload["data"] = data
}
payload[type] = to
return payload
}
test.ts
import { PNotification, buildPushNotification, SenderType } from "./MessageTemplate";
let notification = new PNotification("title 1", "body 1")
let data = {
id:1024,
name:"Juan"
}
let to = "AISA143f43533d32d3243d546fwf234"
let topic = "global"
let payload1 = buildPushNotification(SenderType.token, to, notification)
console.log(payload1)
functions / tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2015"
},
"compileOnSave": true,
"include": [
"src"
]
}
答案 0 :(得分:0)
在本地运行时,由于未使用tsconfig.json
,因此编译器所使用的严格设置比尝试推送时使用的严格设置少,在存在tsconfig.json
的情况下,其中包含一些可用的严格选项启用,即strict
,noImplicitReturns
和noUnusedLocals
。
这是tsconfig.json
和the compiler options的文档
要显示在本地成功编译的文件中的strict
错误,我已将它们添加到TypeScript Playground中的单个文件中。如果单击Options
按钮并启用严格选项,则会看到错误。
这里是another Playground with the errors removed。
要解决payload[...]
左侧错误,我添加了一个界面:
interface Payload {
notification?: any
data?: any
token?: string
topic?: string
condition?: string
}
这为每种可能的属性定义了类型,它们都是可选的,因为它们包括?
。
payload
声明现在变为:
let payload: Payload = {}
由于payload
及其属性已被键入,noImplicitAny
错误已得到修复。
要解决notification.toJSON
右侧错误,我添加了类型保护,以确保notification
是正确的类型:
if (!isNullOrUndefined(notification)) {
if (notification instanceof PNotification) {
payload.notification = notification.toJSON()
}
}
您可能希望更改isNullOrUndefined
功能,而不要使用此功能。如果是这样,则可以使用用户定义的类型保护(请参见here和here)。
要解决其他错误,我建议您使用vscode或类似的方法,并使用与Firebase相同的tsconfig.json
,以便您可以在本地查看错误并在编辑器中显示潜在的修复程序。另外,如果您愿意并且可以的话,可以从Firebase tsconfig.json
中删除严格的设置。