BahaiResearch.com BahaiResearch.com - 1 month ago 10
Javascript Question

Typescript typings definition of a property

Newbie question

I have a Javascript object which stores a dictionary of words:

var Words = {};
Words.Account="Account";
Words.Account_Login="Account Login";


My Typescript definition looks like this:

declare class Words {
Account: string;
Account_Login: string;


To use it in my TypeScript I have to use "prototype". How can I not have to use prototype?

/// <reference path="../typings/words.d.ts" />
var abc = Words.prototype.Account_Login;

Answer

In your example you are accessing Account_Login through the Words class as if it is a static variable.

If you are creating a class Words then I would expect that you would be using it something like this:

var myWords = new Words();
var abc = myWords.Account_Login;

Maybe instead of a class definition you want to define an interface like the following:

declare interface IWords{
    Account: string;
    Account_Login: string;
}

Then taking your Words object make sure that you are using the interface that you defined

var myWords = Words as IWords;
var abc = myWords.Account_Login;