Typescript 当打字似乎存在时,围绕打字脚本错误的混淆

Typescript 当打字似乎存在时,围绕打字脚本错误的混淆,typescript,react-native,react-native-firebase,Typescript,React Native,React Native Firebase,我正在从事一个react native项目,该项目利用包react native firebase作为提供远程和本地通知的解决方案 如果我做到以下几点,一切都会好起来: import firebase from 'react-native-firebase' ... const messagingPermission = await firebase.messaging().hasPermission() 上述代码运行良好,无需投诉。但是, import firebase from 'rea

我正在从事一个
react native
项目,该项目利用包
react native firebase
作为提供远程和本地通知的解决方案

如果我做到以下几点,一切都会好起来:

import firebase from 'react-native-firebase'

...
const messagingPermission = await firebase.messaging().hasPermission()
上述代码运行良好,无需投诉。但是,

import firebase from 'react-native-firebase'

...
const customNotification = new firebase.notifications.Notification()
抱怨

[ts] Property 'Notification' does not exist on type '{ (): Notifications; 
nativeModuleExists: boolean }'
但是,如果我按照键入(ctrl+单击)进行操作,那么我可以在那里看到类
通知
,并将其键入

我是错过了一堂重要的打字课,还是抓住了我忽略的一点?我肯定这个包是正确输入的,那么这是怎么回事

如果您认为有帮助,我可以提供我的
tsconfig.json

{
    "compilerOptions": {
        "target": "es2015",
        "module": "es2015",
        "moduleResolution": "node",
        "allowSyntheticDefaultImports": true,
        "noImplicitAny": false,
        "preserveConstEnums": true,
        "allowJs": false,
        "sourceMap": true,
        "noImplicitReturns": true,
        "noUnusedParameters": true,
        "noUnusedLocals": true,
        "watch": false,
        "skipLibCheck": true,
        "jsx": "react",
        "outDir": "artifacts",
        "rootDir": "src",
        "baseUrl": "src",
  },
  "filesGlob": [
      "typings/index.d.ts",
      "app/**/*.ts",
      "app/**/*.tsx"
  ],
  "types": [
      "react",
      "react-native",
      "jest"
  ],
  "exclude": [
      "android",
      "ios",
      "build",
      "node_modules"
  ],
  "compileOnSave": true
}

编辑:添加了tsconfig

尝试调用
通知
作为函数:
firebase.notifications().Notification()


为什么?

查看错误消息:

[ts] Property 'Notification' does not exist on type '{ (): Notifications; 
nativeModuleExists: boolean }'
这意味着
firebase.notifications
的类型为
{():notifications;nativeModuleExists:boolean}

属性
():通知
是什么意思?在接口中,没有名称的属性
()
表示接口本身是可调用的/表示一个函数,在这个函数中返回一个
通知
对象。这些函数还可以具有其他属性,例如这里的
nativeModuleExists
,因为函数是JavaScript中的一级对象


无论如何,错误消息准确地告诉我们出了什么问题,但它用于这样做的语法有点模糊——这使得本质上的打字错误看起来更复杂。

尝试调用
通知作为函数:
firebase.notifications().Notification()


为什么?

查看错误消息:

[ts] Property 'Notification' does not exist on type '{ (): Notifications; 
nativeModuleExists: boolean }'
这意味着
firebase.notifications
的类型为
{():notifications;nativeModuleExists:boolean}

属性
():通知
是什么意思?在接口中,没有名称的属性
()
表示接口本身是可调用的/表示一个函数,在这个函数中返回一个
通知
对象。这些函数还可以具有其他属性,例如这里的
nativeModuleExists
,因为函数是JavaScript中的一级对象


不管怎么说,错误消息准确地告诉我们出了什么问题,但是它用来这样做的语法有点模糊——这使得本质上的打字错误看起来更复杂。

这看起来像是我们的疏忽——我们错过了Typescript定义中的静态类型。我刚刚推出了一个修复程序,将在今天晚些时候作为v4.0.2的一部分发布:

这看起来像是我们的疏忽——我们错过了Typescript定义中的静态类型。我刚刚推出了一个补丁,它将在今天晚些时候作为v4.0.2的一部分发布:

在这种情况下,
tsconfig.json
可能很有用。如果你能提供,那就太好了…添加了
tsconfig.json
:)@Aleski如果其中一个答案有帮助,你能把一个标记为接受吗?(或者,如果你想澄清的话,请发表评论)我的错,是的,它是由Chris在下面排序的(不是在4.0.2中,而是在经过一些故障排除后最终在4.0.4中)。在这种情况下,
tsconfig.json
可能有用。如果你能提供,那就太好了…添加了
tsconfig.json
:)@Aleski如果其中一个答案有帮助,你能把一个标记为接受吗?(或评论,如果你想澄清的话)我不好,是的,它是由Chris在下面排序的(不是在4.0.2中,但经过一些故障排除后最终在4.0.4中)