【发布时间】: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