Type of the module singleton instance

Topics: Language Specification
Jul 1, 2013 at 7:00 PM
Greetings!

I am updating typings for Raphael (svg library) to work with 0.9. It has a certain property that I can't find an easy work around for. Elements keep references to the singleton module instance, so there's a need to type this accessor instance var, but as of 0.9 declaring a module no longer creates a type of the same name.
declare module Raphael {
  ...
}

interface RaphaelElement {
  ...
  raphael: Raphael;
  ...
}
I read the spec a few times, and googled around. So far it seems that my only option is to declare RaphaelStatic as an interface, and do the pre-0.9 style declare var Raphael: RaphaelStatic. Is there really no better way?
Coordinator
Jul 1, 2013 at 10:47 PM
We're considering a 'typeof' that would let you get at the shape of a module. For example:
declare module Raphael {
  ...
}

interface RaphaelElement {
  ...
  raphael: typeof Raphael;
  ...
}
Jul 1, 2013 at 10:53 PM
Thought about this for a bit, sounds quite reasonable. Is there a timeline for this? Days, weeks, or months? Trying to decide how hacky of a workaround I will put up with until the feature is implemented.
Jul 4, 2013 at 4:24 PM
Edited Jul 4, 2013 at 4:25 PM
Perhaps it could be intresting to do something like this also :
class Person {
    name:string
}

function doSomething<T>(obj : T, property: propertyof T) {
}

var p = new Person();

doSomething(p, "age"); // error

Jul 5, 2013 at 2:35 AM
@fdecampredon

I don't understand what you're trying to illustrate. What use case is there for propertyof? How does propertyof choose which (type of) property of the class should be given to the function?

E.g. what happens if person is declared as follows:
class Person {
    name: string;
    age: number;
}
How does it choose between the name/string or age/number as a valid input?
Jul 7, 2013 at 12:57 PM
Edited Jul 8, 2013 at 10:10 AM
The idea is to force a string to be the name of the property of a type, that allows to prevent bug of misspelled property name.
for example a backbone like model could be typed like this :

declare class Model<T> {
    ...
    set(property: propertyof T, value : any);
    get(property: propertyof T) : any;
    ....
}

...
interface IPerson {
    name : string;
}

class Person extends BackBone.Model<IPerson> {
....
}

var person = new Person();

person.set("name" , "Jhon"); // ok
person.set("age", 19);  // error age is not a property of the IPerson interface.

from this example i can also see that a way to strongly type the value could be interesting, but i don't see a simple way.
Jul 8, 2013 at 2:40 AM
Ooh, I get it.

The string is how you'd access a property if you were using an array notation, e.g. person["name"].

Yeah, I can see that being useful :).