【问题标题】:Confusion around typescript errors when typings seemingly exist当打字似乎存在时,对打字稿错误的困惑
【发布时间】:2018-04-11 15:40:28
【问题描述】:

我正在开发一个 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+click)进行操作,那么我可以在那里看到类 Notifications 并输入。

我在这里错过了重要的 Typescript 课程还是我忽略了问题?我确定包的输入正确,所以这里发生了什么?

如果您认为有帮助,我可以提供我的 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

【问题讨论】:

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

标签: typescript react-native react-native-firebase


【解决方案1】:

这看起来像是我们的疏忽——我们错过了 Typescript 定义中的静态类型。我刚刚推出了一个修复程序,它将在今天晚些时候作为 v4.0.2 的一部分登陆:https://github.com/invertase/react-native-firebase/commit/590d69ce8985842e830c234e18d020efc98e76c8

【讨论】:

    【解决方案2】:

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


    为什么?

    查看错误信息:

    [ts] Property 'Notification' does not exist on type '{ (): Notifications; 
    nativeModuleExists: boolean }'
    

    这意味着firebase.notifications{ (): Notifications; nativeModuleExists: boolean } 类型。

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

    无论如何,错误消息准确地说明了哪里出了问题,但它使用的语法有点晦涩 - 使本质上是错字的问题看起来像一个更复杂的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多