IDBObjectStore 接口上的 Typescript 1.8 lib.d.ts 缺少自动增量 属性?
Typescript 1.8 lib.d.ts missing autoIncrement property on IDBObjectStore Interface?
如果我查看以下页面的规范,显然在 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.8 .
该文件似乎只是缺少有问题的 属性。这是该文件中的接口定义:
// Excerpt from C:\Program Files (x86)\Microsoft SDKs\TypeScript.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 接口上(可选)。
关于此问题的任何见解以及健康的解决方法可能是什么?我有点莫名其妙。
提前致谢。
也许您需要升级您的定义。可以看到 autoIncrement
属性 定义在 the Typescript repo. If you look at the commit history, it was added on 2016-02-23.
您可以像@miqid 建议的那样在运行时扩展接口。我通常在 src 文件夹中放一个 lib.d.ts
,像这样:
declare module IDBObjectStore {
const autoIncrement: any;
}
我用它来扩展 CodeMirror typedef,因为 属性 我的 def 丢失了。不确定您的 d.ts 是否以与我相同的方式定义,但是 Google(和 SO)上有很多关于扩展打字稿接口的内容。
如果我查看以下页面的规范,显然在 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.8 .
该文件似乎只是缺少有问题的 属性。这是该文件中的接口定义:
// Excerpt from C:\Program Files (x86)\Microsoft SDKs\TypeScript.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 接口上(可选)。
关于此问题的任何见解以及健康的解决方法可能是什么?我有点莫名其妙。
提前致谢。
也许您需要升级您的定义。可以看到 autoIncrement
属性 定义在 the Typescript repo. If you look at the commit history, it was added on 2016-02-23.
您可以像@miqid 建议的那样在运行时扩展接口。我通常在 src 文件夹中放一个 lib.d.ts
,像这样:
declare module IDBObjectStore {
const autoIncrement: any;
}
我用它来扩展 CodeMirror typedef,因为 属性 我的 def 丢失了。不确定您的 d.ts 是否以与我相同的方式定义,但是 Google(和 SO)上有很多关于扩展打字稿接口的内容。