【问题标题】:Typescript 1.8 lib.d.ts missing autoIncrement property on IDBObjectStore Interface?Typescript 1.8 lib.d.ts 在 IDBObjectStore 接口上缺少 autoIncrement 属性?
【发布时间】:2016-06-30 22:49:22
【问题描述】:

如果我查看以下页面的规范,显然 IDBObjectStore 上有一个指定的属性(只读),称为“autoIncrement”:

https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore

https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/autoIncrement

但是,当尝试从 Visual Studio 2015 中读取该属性或尝试使用 Typescript 编译器进行编译时,该属性被标记为错误:

// Excerpted from the code I am writing to manage our IndexedDB Schemas.
interface ISchemaForIndex {
    keyPath: string;
    name: string;
    unique: boolean;
    multiEntry: boolean;
}

interface ISchemaForObjectStore {
    clearStoreOnUpgradeBeforeVersion: number;
    name: string;
    keyPath: string;
    autoIncrement: boolean;
    indexes: ISchemaForIndex[];
}

function getOrCreateOrReCreateStore(upgradeDb: IDBDatabase, transaction: IDBTransaction, oldVersion: number, schemaObjectStore: ISchemaForObjectStore) {

    if (_.contains(upgradeDb.objectStoreNames, schemaObjectStore.name)) {
        if (oldVersion >= schemaObjectStore.clearStoreOnUpgradeBeforeVersion) {
            const objectStore = transaction.objectStore(schemaObjectStore.name);
            if (objectStore.keyPath === schemaObjectStore.keyPath &&

                // NEXT LINE HAS ERROR ON ATTEMPT TO READ autoIncrement PROPERTY FROM objectStore
                objectStore.autoIncrement === schemaObjectStore.autoIncrement) {

                return objectStore;
            }
        }
        upgradeDb.deleteObjectStore(schemaObjectStore.name);
    }
    return upgradeDb.createObjectStore(schemaObjectStore.name,
    { keyPath: schemaObjectStore.keyPath, autoIncrement: schemaObjectStore.autoIncrement });
}

似乎有问题的接口是在 lib.d.ts 中定义的,在我的系统中的文件夹 C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.8 中找到。

该文件似乎只是缺少相关属性。这是该文件中的接口定义:

// Excerpt from C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.8\lib.d.ts
interface IDBObjectStore {
    indexNames: DOMStringList;
    keyPath: string;
    name: string;
    transaction: IDBTransaction;
    add(value: any, key?: any): IDBRequest;
    clear(): IDBRequest;
    count(key?: any): IDBRequest;
    createIndex(name: string, keyPath: string | string[], optionalParameters?: IDBIndexParameters): IDBIndex;
    delete(key: any): IDBRequest;
    deleteIndex(indexName: string): void;
    get(key: any): IDBRequest;
    index(name: string): IDBIndex;
    openCursor(range?: any, direction?: string): IDBRequest;
    put(value: any, key?: any): IDBRequest;
}

找不到属性 autoIncrement。

有趣的是,在同一个文件中,IDBObjectStoreParameters 接口上确实存在(可选)该属性。

关于这个问题是什么以及可能的健康解决方法有什么见解吗?我有点困惑。

提前致谢。

【问题讨论】:

  • 您可以尝试在您的 TS 项目中创建一个补充类型定义(即foo.d.ts)文件,链接它(即/// <reference path="foo.d.ts" />)并在另一个interface IDBObjectStore 定义中定义autoIncrement 属性。 TypeScript 应该将接口合并在一起,这应该有望消除该属性错误(参见declaration merging

标签: typescript indexeddb


【解决方案1】:

您可以在运行时扩展接口,就像@miqid 建议的那样。我通常在 src 文件夹中放一个lib.d.ts,如下所示:

declare module IDBObjectStore {
    const autoIncrement: any;
}

我使用它来扩展我的定义缺失的属性的 CodeMirror 类型定义。不确定您的 d.ts 是否以与我相同的方式定义,但 Google(和 SO)上有很多关于扩展 typescript 接口的内容。

【讨论】:

  • 酷,谢谢。最近才开始使用 Typescript,我一直没有意识到标准 typedef 缺少元素是很常见的。不确定这是否是“我们的答案”,或者我们是否会遵循下面@morphatic 的建议......这两个中的一个将很快被标记为“答案”!
  • 我们开发团队的共识是停止使用 typings 实用程序并将 lib.d.ts 置于源代码控制之下。谢谢。
  • 如果您建议以某种方式将所有 typedefs 放入该文件中,我强烈建议您不要这样做。必须维护这些信息将是推进工作的一块沉重的石头。如果问题是例如构建服务器网络访问,则只需将类型文件夹检查到 src 中,或者设置一个 npm 镜像(有关如何执行此操作的大量信息)。拥有一个 lib.d.ts 文件对我来说大多是为了克服现在的障碍,或者对于小补丁,而不是批发定义。
【解决方案2】:

也许您需要升级您的定义。可以看到autoIncrement 属性是在the Typescript repo 中定义的。如果您查看提交历史记录,it was added on 2016-02-23

【讨论】:

  • 嗯。有趣的。几天前,我刚刚通过 Visual Studio 升级到 Typescript 1.8,并想象我会从 Microsoft 获得最新版本。我猜不会。谢谢。我最近才来 Typescript,对 Typedef 中这种缺失的元素不太熟悉。 (在 .NET 中不常见!)不确定我们的团队是否会使用您的答案或 @Svend 作为“答案”,但其中一个将在接下来的几天内被标记为“答案”。谢谢。
猜你喜欢
  • 2016-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-15
  • 2015-12-02
  • 2019-02-07
  • 2019-04-25
相关资源
最近更新 更多