Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IDBKeyRange definition is degraded #1075

Closed
falsandtru opened this issue Nov 6, 2014 · 20 comments
Closed

IDBKeyRange definition is degraded #1075

falsandtru opened this issue Nov 6, 2014 · 20 comments
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Revisit An issue worth coming back to

Comments

@falsandtru
Copy link
Contributor

IDBKeyRange is defined as follows.

https://github.com/Microsoft/TypeScript/blob/master/bin/lib.d.ts#L11630

interface IDBKeyRange {
    upper: any;
    upperOpen: boolean;
    lower: any;
    lowerOpen: boolean;
}
declare var IDBKeyRange: {
    prototype: IDBKeyRange;
    new(): IDBKeyRange;
    bound(lower: any, upper: any, lowerOpen?: boolean, upperOpen?: boolean): IDBKeyRange;
    only(value: any): IDBKeyRange;
    lowerBound(bound: any, open?: boolean): IDBKeyRange;
    upperBound(bound: any, open?: boolean): IDBKeyRange;
}

But it does not work.

new IDBKeyRange(); // error

Please recovery.

interface IDBKeyRange {
    upper: any;
    upperOpen: boolean;
    lower: any;
    lowerOpen: boolean;
    bound(lower: any, upper: any, lowerOpen?: boolean, upperOpen?: boolean): IDBKeyRange;
    only(value: any): IDBKeyRange;
    lowerBound(bound: any, open?: boolean): IDBKeyRange;
    upperBound(bound: any, open?: boolean): IDBKeyRange;
}
@DickvdBrink
Copy link
Contributor

Except for the newable contructor the current one is right.
IDBKeyRange.only is like a static method and does not belong on the interface

@falsandtru
Copy link
Contributor Author

How do I access to IDBKeyRange.only doing?

new IDBKeyRange().only // Runtime error
IDBKeyRange.only // Compile error

We can no longer access the IDBKeyRange method in the regular procedure.

@DickvdBrink
Copy link
Contributor

IDBKeyRange.only("1"); works for me on the commandline (typescript 1.1.0.1 from NPM).
It doesn't work in Visual Studio using the 1.1 extension though, so there is something fishy there

@falsandtru
Copy link
Contributor Author

When you have installed the VisualStudio, TypeScript that has been installed by npm is ignored.
In fact you may have used an older version of TypeScript.
Modification is required. Are you modify it?

@DickvdBrink
Copy link
Contributor

@falsandtru, I installed the CTP for 1.1 for VS link

In short: npm 1.1 works
VS with 1.1 CTP doesn't work with intellisense and doesn't build.
VS with 1.0 you need to add the following to a file in your project ( I called mine extensions.d.ts and added the stuff from below)

declare var IDBKeyRange: {
    bound(lower: any, upper: any, lowerOpen?: boolean, upperOpen?: boolean): IDBKeyRange;
    only(value: any): IDBKeyRange;
    lowerBound(bound: any, open?: boolean): IDBKeyRange;
    upperBound(bound: any, open?: boolean): IDBKeyRange;
}

For clarification what are you using? Visual Studio version (if you use that) and TypeScript version?
I did not change lib.d.ts because that would cause issues with other guys in my team

@falsandtru
Copy link
Contributor Author

I am fails to compile in tsc command from cmd.

Windows7 64bit
VS2013 WEB Express (not install CTP extension)
Nodejs 0.10.32
typescript 1.1.0-1 (npm)

@mhegazy
Copy link
Contributor

mhegazy commented Nov 6, 2014

@DickvdBrink something sounds strange, 1.1 CTP did not update the language service. so the language service is the same as you had in 1.0. so there should not be a change in behavior here. what are the errors are you seeing, and what is the definition of IDBKeyRange you have?

@falsandtru can you share the code that is failing to compile, along with the error you are getting?

This is building for me on 1.1, and latest from master.

IDBKeyRange.only("1");

@DickvdBrink
Copy link
Contributor

@mhegazy, when using VS 14 CTP with 1.1 it works like a charm, IDBKeyrange has the interface and the declare var as shown by @falsandtru in his first post. This is a Machine running Windows 10 Preview and only has VS 14 on it.

Different machine Windows 8.1 with VS 2012 and 2013 and installed multiple versions on it coming from TypeScript 0.8.3 if I recall correctly is giving me the error Could not find symbol "IDBKeyRange".
VS 2013 (Ultimate) Update 3 with CTP 1.1 is loading the lib.d.ts from C:\MicroSd\Programs\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\TypeScript and only has the interface as shown below:

interface IDBKeyRange {
    upper: any;
    upperOpen: boolean;
    lower: any;
    lowerOpen: boolean;
    bound(lower: any, upper: any, lowerOpen?: boolean, upperOpen?: boolean): IDBKeyRange;
    only(value: any): IDBKeyRange;
    lowerBound(bound: any, open?: boolean): IDBKeyRange;
    upperBound(bound: any, open?: boolean): IDBKeyRange;
}

When I look in the microsoft sdks\typescript folder I have the 1.0 and the 1.1 folder both with a lib.d.ts.
The 1.0 has the same interface as in this post and the 1.1 as found in the Topic Start.

Note: I did install the CTP for VS 2013 (it is showing me the warning 'Your project file uses a newer version of the TypeScript compiler...blabla')

I never edited lib.d.ts manually or something and also tried to run a repair from the 1.1 CTP installer.
(Haven't tried to repair VS 2013 though)

Also: it is pretty funny because with the 1.0 release I added a extensions.d.ts file with the declare var IDBKeyRange stuff so I could use it with 1.0 and with 1.1 with that enabled it first whines about subsequent variable declarations must have the same type. and after disabling that it says `Could not find symbol``IDBKeyRange```

edited this post It didn't contain the error I was having .. xD

@falsandtru
Copy link
Contributor Author

I will cancel the update. Please wait a little.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 6, 2014

OK. now i see the problem..

you are redefining the var IDBKeyRange. The language does not allow changing the type of a var after it has been declared, this is diffrent from other constructs like an interface that can be augmented in later declarations. so something like:

var x: number;
var x: string;  /// error TS2403: Subsequent variable declarations must have the same type.  
                ///     Variable 'x' must be of type 'number', but here has type 'sting'.

what you want is for the definition of the var IDBKeyRange to define a new type that you can reopen later on, this is covered by #182.

the soultion here would be:

// lib.d.ts would define:
interface IDBKeyRange {
    upper: any;
    upperOpen: boolean;
    lower: any;
    lowerOpen: boolean;
}
interface IDBKeyRangeStatic {
    prototype: IDBKeyRange;
    new(): IDBKeyRange;
    bound(lower: any, upper: any, lowerOpen?: boolean, upperOpen?: boolean): IDBKeyRange;
    only(value: any): IDBKeyRange;
    lowerBound(bound: any, open?: boolean): IDBKeyRange;
    upperBound(bound: any, open?: boolean): IDBKeyRange;
}

declare var IDBKeyRange: IDBKeyRangeStatic;


// your file would update the static side
interface IDBKeyRangeStatic {
    myNewStaticHelperFunction();
}

Now for the next issue, defining the function on the interface is not probably what you mean. so if you look at the definition, there are two things:

  1. interface IDBKeyRange, this defines the instance side of IDBKeyRange. i.e. objects of that type. i.e. things you get after calling new IDBKeyRange();
    this describes the runtime augmentation IDBKeyRange.prototype.blah = 3; that sets all instances of IDBKeyRange to have a property 'blah' with value 3;
  2. var IDBKeyRange, this defines the static side of IDBKeyRange, i.e. the constructor object, i.e. the value you mean when you reference "IDBKeyRange". this describes the runtime augmentation IDBKeyRange.blah = 3; which adds a property blah, on the value IDBKeyRange.
var instance = new IDBKeyRange();
instance.upper;   // upper bounds of the value "instance", whose this object is "instance"

IDBKeyRange.upperBound // a static helper function that has no state associated with it.

adding upperBound on the interface, makes all instances of IDBKeyRange, have that, which is not what you want. what you want is a helper function on the static value side of IDBKeyRange.

hope that helps.

@DickvdBrink
Copy link
Contributor

@mhegazy, should I creatie a new issue for the probleem i'm seeing with vs2013 because I think this one is a bit noisy (which is my fault, sorry bout that)

@mhegazy
Copy link
Contributor

mhegazy commented Nov 6, 2014

@DickvdBrink yes please. thanks for reporting this issue!

@falsandtru
Copy link
Contributor Author

Wait, Duplication seems to be due to previous declaration that I do not show. It is not a Duplication of the definition file. Please anymore let a while verification.

@falsandtru
Copy link
Contributor Author

The essential cause is I was found.
In the past examples of cancels all.

There are the following unnatural constraints. Is this would specification?

When IDBKeyRange I referring to the directly, I can use the method.
But it can not be referenced from a variable or class that was explicitly the type.
Perhaps this is I reproduce in other people.

Can not be defined only by inference the type of a variable.

IDBKeyRange.only('1'); // pass
var alias1 = IDBKeyRange;
alias1.only("1"); // pass
alias1.upperBound(1); // pass

var alias2: IDBKeyRange = IDBKeyRange; // error
error TS2322: Type '{ new (): IDBKeyRange; prototype: IDBKeyRange; bound(lower: any, upper: ny, lowerOpen?: boolean,...' is not assignable to type 'IDBKeyRange':  Property 'upper' is missing in type '{ new (): IDBKeyRange; prototype: IDBKeyRange; bound(lower: any, upper: any, lowerOpen?: boolean,...'.

Impossible to resolve the vendor prefix.

interface msIDBKeyRange extends IDBKeyRange {}
declare var msIDBKeyRange: IDBKeyRange;
var keyrange = IDBKeyRange || msIDBKeyRange;
keyrange.only('1'); // error
error TS2339: Property 'only' does not exist on type '{}'

Impossible to define the class interface.

class IDatabase {
  keyrange: IDBKeyRange
}

class Database implements IDBDatabase { // error
  keyrange = IDBKeyRange
}

var db: IDatabase = new Database();
db.keyrange.only(1);
error TS2421: Class 'Database' incorrectly implements interface 'IDBDatabase':  Property 'version' is missing in type 'Database'.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 7, 2014

The issue here is a name can exist as a type and as a value. IDBKeyRange is both a var, and an interface. in type position (that is after the : in keyrange: IDBKeyRange, it is the type, but in keyrange = IDBKeyRange it is the value.

To create a new IDBKeyRange Object you must use one of the static helpers:

  • IDBKeyRange .bound
  • IDBKeyRange.only
  • IDBKeyRange.lowerBound
  • IDBKeyRange.upperBound

here is how you can get your samples to work.

class IDatabase {
    // keyrange is an instance of this type, a specific one.
    keyrange: IDBKeyRange
}

class Database implements IDatabase {
    // now create an instance of IDBKeyRange, either using bound, only, lowerBound, or upperBound.
    keyrange = IDBKeyRange.bound("A", "Z");
}

var db: IDatabase = new Database();
// this is the same instance you created when you created the object db
db.keyrange.upper; ///  === "Z"


// now let's create a new instance
var newKey = IDBKeyRange.only("1");
newKey.lower; /// === 1
newKey.upper; /// === 1

var myKey: IDBKeyRange = newKey; // OK
myKey.lower; /// === 1, still
myKey.upper; /// === 1, still

@mhegazy mhegazy added the Bug A bug in TypeScript label Nov 7, 2014
@mhegazy
Copy link
Contributor

mhegazy commented Nov 7, 2014

marking it as a bug, as we need to remove the construct signature off IDBKeyRange

@falsandtru
Copy link
Contributor Author

Thanks!
But we are still left with the problem.

We can not resolve the vendor prefix.
Also, we can not hold it even resolved.
Vendor prefix of resolution becomes very painful.
After all, it has been bothering me.

// Need
class Database {
  keyrange = window.IDBKeyRange || window.msIDBKeyRange
}
var db = new Database();
var keyrange = db.keyrange.only(1); // success!
// Now
class Database {
  // keyrange type is not IDBKeyRange.
  keyrange = window.IDBKeyRange || window.msIDBKeyRange
}
var db = new Database();
// Impossible to access the IDBKeyRange static method.
var keyrange = db.keyrange.only(1); // error...
// Ideal
class IDatabase {
  keyrange: IDBKeyRange
}

class Database implements IDBDatabase {
  keyrange = window.IDBKeyRange || window.webkitIDBKeyRange || window.mozIDBKeyRange || window.msIDBKeyRange
}

var db: IDatabase = new Database();
var keyrange = db.keyrange.only(1);

:'-(

@danquirk
Copy link
Member

danquirk commented Nov 7, 2014

You can define msIDBKeyRange yourself with an ambient declaration and reopen the Window interface to include it there if you want. The type of keyrange after assigning window.IDBKeyRange || window.msIDBKeyRange will depend on the exact shape of each type. It's possible you will need to write a type annotation or type assertion to get the desired type.

@falsandtru
Copy link
Contributor Author

I am now, I have to resolve this problem in their own definition.
However, the maneuverability is required in association with the vendor prefix, it is unrealistic in the non-efficiency of all of the people must be resolved on their own, respectively.

// User customization
declare var webkitIndexedDB: IDBFactory;
declare var mozIndexedDB: IDBFactory;
declare var msIndexedDB: IDBFactory;
declare var webkitIDBKeyRange: IDBKeyRange;
declare var mozIDBKeyRange: IDBKeyRange;
declare var msIDBKeyRange: IDBKeyRange;
interface Window {
  IDBKeyRange: IDBKeyRange
  webkitIDBKeyRange?: IDBKeyRange
  mozIDBKeyRange?: IDBKeyRange
  msIDBKeyRange?: IDBKeyRange
}
// Should vendor
interface IDBKeyRange {
  bound(lower: any, upper: any, lowerOpen?: boolean, upperOpen?: boolean): IDBKeyRange;
  only(value: any): IDBKeyRange;
  lowerBound(bound: any, open?: boolean): IDBKeyRange;
  upperBound(bound: any, open?: boolean): IDBKeyRange;
}

@falsandtru
Copy link
Contributor Author

http://www.w3.org/TR/IndexedDB/#range-concept

interface IDBKeyRange {
readonly attribute any lower;
readonly attribute any upper;
readonly attribute boolean lowerOpen;
readonly attribute boolean upperOpen;
static IDBKeyRange only (any value);
static IDBKeyRange lowerBound (any lower, optional boolean open);
static IDBKeyRange upperBound (any upper, optional boolean open);
static IDBKeyRange bound (any lower, any upper, optional boolean lowerOpen, optional boolean upperOpen);

};

// lib.d.ts at master branch head
interface IDBKeyRange {
    upper: any;
    upperOpen: boolean;
    lower: any;
    lowerOpen: boolean;
    // Missing method
    // bound(lower: any, upper: any, lowerOpen?: boolean, upperOpen?: boolean): IDBKeyRange;
    // only(value: any): IDBKeyRange;
    // lowerBound(bound: any, open?: boolean): IDBKeyRange;
    // upperBound(bound: any, open?: boolean): IDBKeyRange;
}
'upper' in IDBKeyRange; // false
'upperOpen' in IDBKeyRange; // false
'upperBound' in IDBKeyRange; // true

@sophiajt sophiajt added this to the TypeScript 1.4 milestone Nov 10, 2014
@mhegazy mhegazy added the Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript label Dec 2, 2014
@mhegazy mhegazy modified the milestones: TypeScript 1.5, TypeScript 1.4 Dec 2, 2014
@mhegazy mhegazy modified the milestones: TypeScript 1.5, TypeScript 1.6 Feb 5, 2015
@mhegazy mhegazy modified the milestones: TypeScript 1.6, TypeScript 1.5 Feb 5, 2015
@mhegazy mhegazy added the Revisit An issue worth coming back to label Mar 24, 2015
@mhegazy mhegazy assigned zhengbli and unassigned mhegazy Apr 7, 2015
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Apr 17, 2015
@mhegazy mhegazy closed this as completed Apr 17, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Revisit An issue worth coming back to
Projects
None yet
Development

No branches or pull requests

6 participants