简体   繁体   中英

Typescript typings definition of a property

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;

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;

@tj-rockefeller is correct, I also found if I also just made the Words static, I could use them as intended.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM