我有一个 JavaScript 对象,它存储单词字典:
var Words = {};
Words.Account="Account";
Words.Account_Login="Account Login";
我的 Typescript 定义如下所示:
declare class Words {
Account: string;
Account_Login: string;
}
要在我的 TypeScript 中使用它,我必须使用“原型(prototype)”。我怎样才能不必使用原型(prototype)?
/// <reference path="../typings/words.d.ts" />
var abc = Words.prototype.Account_Login;
最佳答案
在您的示例中,您通过 Words 类访问 Account_Login,就好像它是静态变量一样。
如果您正在创建一个类Words
,那么我希望您会像这样使用它:
var myWords = new Words();
var abc = myWords.Account_Login;
也许您想要定义如下所示的接口(interface),而不是类定义:
declare interface IWords{
Account: string;
Account_Login: string;
}
然后使用您的 Words 对象确保您正在使用您定义的接口(interface)
var myWords = Words as IWords;
var abc = myWords.Account_Login;
关于javascript - Typescript 键入属性的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40028366/