简体   繁体   English

如何访问在d.ts文件中声明的枚举-Nativescript

[英]How to access enum declared in a d.ts file - Nativescript

I have declared an enum in a d.ts file 我已经在d.ts文件中声明了一个枚举

declare enum myenum
{
    TYPE_ONE,
    TYPE_TWO,
}

declare class MobileAPI extends NSObject {....}

Now I am accessing the enum in a .ts file like bellow. 现在,我正在像下面这样的.ts文件中访问枚举。

this.mobileAPI.addType(myenum.TYPE_ONE,NSString.stringWithString("someString"));

I can compile the app, but getting following JS error 我可以编译应用程序,但出现以下JS错误

JS ERROR ReferenceError: Can't find variable: myenum

What's am I doing wrong here, please share some idea on this 我在这里做错了什么,请对此分享一些想法

What's am I doing wrong here, please share some idea on this 我在这里做错了什么,请对此分享一些想法

You are creating a declaration . 您正在创建一个声明 These should only be used if you have corresponding JavaScript code available . 仅当您有相应的JavaScript代码可用时,才应使用它们。

Fix 固定

Dont use declare and use a file .ts (not .d.ts ) 不要使用声明,而要使用文件.ts (不是.d.ts

enum myenum // Fixed
{
    TYPE_ONE,
    TYPE_TWO,
}

More 更多

If you are not the author of the .d.ts file, make sure you load the corresponding JS for the .d.ts file. 如果您不是.d.ts文件的作者,请确保.d.ts文件加载相应的JS

You can do inside your d.ts : 您可以在d.ts内部进行操作:

export const enum myenum {
  TYPE_ONE = "TYPE_ONE",
  TYPE_TWO = "TYPE_TWO"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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