【问题标题】:How to get types for expo-sqlite?如何获取 expo-sqlite 的类型?
【发布时间】:2019-11-26 21:35:50
【问题描述】:

在 expo sdk 的第 33 版中,sqlite 被移到了它自己的包 expo-sqlite,现在我无法加载要加载的类型。

而不是

import {SQLite} from 'expo';

我有

import {SQLite} from 'expo-sqlite';

但是类型没有被加载。

我收到以下类型错误:

Cannot find namespace 'SQLite'.

【问题讨论】:

    标签: typescript react-native expo typescript-typings


    【解决方案1】:

    如果您的问题是从 SQLResultSetRowList 访问数据,那么 我建议你定义一个像 CustomResultset 这样扩展 SQLResultSetRowList 的接口。

    例如:

    import * as SQLite from 'expo-sqlite';
    
    interface CustomResultSet extends SQLite.SQLResultSetRowList {
        _array: [],
    }
    

    这可能不是最佳解决方案,但您应该能够通过 _array 属性访问数据,而不会在代码中出现类型错误。

    【讨论】:

      【解决方案2】:

      expo-sqlite 包中缺少类型。我在 Expo 的 GitHub 页面上打开了一个 issue,问题已经解决了。

      GitHub 问题:https://github.com/expo/expo/issues/5264

      Github 拉取请求:https://github.com/expo/expo/pull/5544

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题并创建了一个类型声明文件,为我解决了这个问题。

        https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html

        我从这个包https://www.npmjs.com/package/@types/expo复制了声明

        这是声明https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/expo/v31/index.d.ts#L2467

        我的项目现在在src 文件夹中有一个expo-sqlite.d.ts 文件,看起来像

        declare module 'expo-sqlite' {
          export namespace SQLite {
            type Error = any;
        
            interface Database {
              transaction(
                callback: (transaction: Transaction) => any,
                error?: (error: Error) => any, // TODO def of error
                success?: () => any
              ): void;
            }
        
            interface Transaction {
              executeSql(
                sqlStatement: string,
                arguments?: string[] | number[],
                success?: (transaction: Transaction, resultSet: ResultSet) => any,
                error?: (transaction: Transaction, error: Error) => any
              ): void;
            }
        
            interface ResultSet {
              insertId: number;
              rowAffected: number;
              rows: {
                length: number;
                item: (index: number) => any;
                _array: HashMap[];
              };
            }
        
            function openDatabase(
              name:
                | string
                | {
                    name: string;
                    version?: string;
                    description?: string;
                    size?: number;
                    callback?: () => any;
                  },
              version?: string,
              description?: string,
              size?: number,
              callback?: () => any
            ): any;
          }
        }
        

        【讨论】:

          【解决方案4】:

          您似乎没有下载该模块。安装模块并应用它。

          npm install expo-sqlite
          

          【讨论】:

          • 我已经安装了模块。代码运行,只是没有正确输入
          • @Daniel 你可以试试npm install flag hub 显示的方法应该是npm
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-08-29
          • 2022-08-04
          • 2019-05-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多