当打字看似存在时,围绕打字错误感到困惑
Confusion around typescript errors when typings seemingly exist
我正在开发一个 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) 然后我可以看到 class 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
尝试调用 notifications
作为函数:firebase.notifications().Notification()
为什么?
查看错误信息:
[ts] Property 'Notification' does not exist on type '{ (): Notifications;
nativeModuleExists: boolean }'
表示firebase.notifications
是{ (): Notifications; nativeModuleExists: boolean }
类型。
属性 (): Notifications
是什么意思?在一个接口中,一个没有名字的属性()
表示接口本身是callable/represents一个函数,在这个函数中[=40] =] 一个 Notifications
对象。这些callable interfaces还可以有其他属性,比如这里的nativeModuleExists
,因为函数是JavaScript.
中的first-class对象
无论如何,错误消息准确地指出了哪里出了问题,但它使用的语法有点晦涩——这使得本质上是错字的问题看起来更复杂。
这看起来像是我们的疏忽 - 我们错过了 Typescript 定义中的静态类型。我刚刚推出了一个修复程序,该修复程序将在今天晚些时候作为 v4.0.2 的一部分发布:https://github.com/invertase/react-native-firebase/commit/590d69ce8985842e830c234e18d020efc98e76c8
我正在开发一个 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) 然后我可以看到 class 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
尝试调用 notifications
作为函数:firebase.notifications().Notification()
为什么?
查看错误信息:
[ts] Property 'Notification' does not exist on type '{ (): Notifications;
nativeModuleExists: boolean }'
表示firebase.notifications
是{ (): Notifications; nativeModuleExists: boolean }
类型。
属性 (): Notifications
是什么意思?在一个接口中,一个没有名字的属性()
表示接口本身是callable/represents一个函数,在这个函数中[=40] =] 一个 Notifications
对象。这些callable interfaces还可以有其他属性,比如这里的nativeModuleExists
,因为函数是JavaScript.
无论如何,错误消息准确地指出了哪里出了问题,但它使用的语法有点晦涩——这使得本质上是错字的问题看起来更复杂。
这看起来像是我们的疏忽 - 我们错过了 Typescript 定义中的静态类型。我刚刚推出了一个修复程序,该修复程序将在今天晚些时候作为 v4.0.2 的一部分发布:https://github.com/invertase/react-native-firebase/commit/590d69ce8985842e830c234e18d020efc98e76c8