【问题标题】:TypeScript + react-native-sqlite-storage => error TS2304: Cannot find name 'Transaction'TypeScript + react-native-sqlite-storage => 错误 TS2304:找不到名称“事务”
【发布时间】:2018-08-01 01:12:51
【问题描述】:

我通过react-native-sqlite-storage 使用 SQLite 并尝试使用 TypeScript。

但是 TypeScript 找不到某些类型。造成问题的代码:

db.transaction((tx: Transaction) => {
  tx.executeSql('SELECT * FROM movies', [], (tx: Transaction, results: ResultSet) => {
    console.log("Query completed");
  });
});

TypeScript 错误:

$ ./node_modules/.bin/tsc --alwaysStrict --skipLibCheck
App.tsx:18:24 - error TS2304: Cannot find name 'Transaction'.

18    db.transaction((tx: Transaction) => {
                          ~~~~~~~~~~~

App.tsx:19:54 - error TS2304: Cannot find name 'Transaction'.

19       tx.executeSql('SELECT * FROM movies', [], (tx: Transaction, results: ResultSet) => {
                                                        ~~~~~~~~~~~

App.tsx:19:76 - error TS2304: Cannot find name 'ResultSet'.

19       tx.executeSql('SELECT * FROM movies', [], (tx: Transaction, results: ResultSet) => {
                                                                              ~~~~~~~~~

这很疯狂,因为Transaction接口定义为here in DefinitelyTyped,所以应该可以找到。正确找到 react-native-sqlite-storage 提供的其他函数的类型。

发生了什么事?怎么可能从同一个类型文件中找到一些类型和另一些类型?

为了重现问题,我所有的项目文件都是here

【问题讨论】:

    标签: typescript react-native


    【解决方案1】:

    缺少的类型是 react-native-sqlite-storage 模块的成员,并且您已经以名称 SQLite 导入了整个模块:

    import SQLite from 'react-native-sqlite-storage'
    

    所以你必须像这样限定类型:

    db.transaction((tx: SQLite.Transaction) => {
      tx.executeSql('SELECT * FROM movies', [], (tx: SQLite.Transaction, results: SQLite.ResultSet) => {
        console.log("Query completed");
      });
    });
    

    就像你合格的SQLite.openDatabase

    【讨论】:

    • 谢谢。我应该想到这一点......我是 TypeScript 的新手,我没有在教程中看到命名空间的东西。
    猜你喜欢
    • 2017-06-23
    • 2023-02-15
    • 2017-03-02
    • 2015-09-19
    • 2020-04-27
    • 2016-12-18
    • 2019-05-06
    • 2019-08-09
    • 1970-01-01
    相关资源
    最近更新 更多