typescript - 创建 NativeScript 插件时的 index.d.ts?
问题描述
我最近开始在 NativeScript 中制作插件 - 遵循文档。我npm run demo.ios
在 src 文件夹中进行了一些成功的运行。
但是,当开始构建我打算使用的实际插件时 - 我遇到了以下错误:
file:///node_modules/tns-core-modules/ui/builder/builder.js:179:0 JS ERROR 错误:从 XML 构建 UI。@app-root.xml:1:1 undefined 不是对象(评估'b.prototype')
ViewBase
我在我的文件中设置了一个扩展方法myplugin.common.ts
,我尝试从演示应用程序调用
问题可能在于index.d.ts
我相信。根据文档myplugin.ios.d.ts
和myplugin.android.d.ts
文件应该在那里复制或手动定义。我正在尝试将myplugin.common.d.ts
文件内容复制到那里。那有效吗?
我还注意到两次运行似乎是必需的 - 因为第一次运行生成的...d.ts.
内容然后必须复制到index.d.ts
.
一些代码...
myplugin.common.ts
declare module "tns-core-modules/ui/core/view-base/view-base" {
interface ViewBase {
myExenstionMethod(myNumber: number): void;
}
}
ViewBase.prototype.myExenstionMethod = function(myNumber: number) {
console.log("myNumber: " + myNumber);
}
和index.d.ts
(复制自myplugin.common.d.ts
):
declare module "tns-core-modules/ui/core/view-base/view-base" {
interface ViewBase {
myExenstionMethod(myNumber: number): void;
}
}
它显示在演示项目中:
错误来自于在我的演示项目中简单地从插件中导入扩展方法:
import "nativescript-myplugin";
笔记
我创建了一个新的插件项目 - 只添加了扩展方法并且它起作用了。所以原来的插件项目中还有其他东西导致了这个错误。不需要添加declare module
。index.d.ts
再打一次
与以前相同的错误 - 从内部实例化 myplugin.ios.ts/myplugin.android.ts - myplugin 类时myplugin.common.ts
。我最初考虑创建一个特定于平台的单例实例以与之交互.common.ts
-但这似乎不起作用?
import { Gestures as iOSGestures} from "./gestures.ios";
import {Gestures as AndroidGestures} from "./gestures.android";
export abstract class Common extends Observable {
private static _instance = null;
static instance(os: string): Common {
if(Common._instance == null) {
/* crash from instantiating iOSGestures or AndroidGestures */
this._instance = os == "iOS" ? new iOSGestures() : new AndroidGestures();
}
return Common._instance;
}
abstract _attach(viewBase: ViewBase, gesture: string): void;
}
奇怪的是,该特定代码从未从任何地方调用过。并注释掉该行,我可以通过简单地包含包含实例化的死代码来创建该错误:
// still inside .common.ts
import { Gestures } from "./gestures.android";
const object = {
methodInFile() {
const g = new Gestures() // <--
}
}
这是android.ts:
export class Gestures extends Common {
_attach(viewBase: ViewBase, gesture: string): void {
console.log("_attach android");
if(gesture == touch.down) {
}
}
}
可能与 index.d.ts 有某种冲突?:
import { Common } from './gestures.common';
export declare class Gestures extends Common {
// define your typings manually
// or..
// take the ios or android .d.ts files and copy/paste them here
}
这是我第一次尝试构建插件 NativeScript - 所以我可能设置完全错误。我希望提供我的插件作为扩展方法ViewBase
- 并且在 common.ts 中定义的扩展方法被成功调用。
// inside common.ts
export class Gestures extends Common {
_attach(): void {
console.log("_attach ios");
}
}
const object = {
methodInFile() {
const g = new Gestures() // <--
}
}
...有效。
但是,当从另一个文件导入该类时,具有new Gestures()
- 会导致上述崩溃。它既不能创建新文件 - ios.common.ts
. 那么可能是npm run demo.ios
在构建时生成了错误的 js 文件?我还完全删除了的内容index.d.ts
- 所以它不会涉及导致错误。
制作common.ts
一个接口 - 在.ios.ts
和中实现android.ts
。我遇到了一个新错误;
file:///app/home/home-page.ts:17:27 JS ERROR TypeError: page.addGestureHandler 不是函数。(在 'page.addGestureHandler("hello")' 中,'page.addGestureHandler' 未定义)
到目前为止,我的结论是我不能将 Common 作为抽象类或接口- 并实例化ios.ts
和android.ts
作为单例。扩展抽象 Common 的类 - 或实现接口 Common - 必须与 (common.ts) 位于同一文件中。这可能与我使用扩展方法作为插件的入口点有关。
解决方案
TypeScript 声明(*.d.ts)
只是为了您的 IDE 支持 IntelliSense。因此,只要您导出要公开的权利定义,就没有所谓的有效或无效。大多数插件只会导出index.d.ts
通常会从*-common.ts
.
更新:
为了防止生成.d.ts
和.js.map
文件,您将declaration
&sourceMap
标志设置src/tsconfig.json
为false
.
您无法避免.js
文件,这是使用 TypeScript 编译器(将 TS 转换为 JS)的唯一原因。在发布您的插件或在演示应用程序中使用它时,.js
文件是实际使用的文件。
之间,没有sourceMap
调试可能对你来说很困难。
推荐阅读
- javascript - 使用 Vue CLI 4 和 A Frame 时出错
- visual-studio - F# VS2019 Windows 窗体
- angular - Angular - 从多个请求中获得响应
- java - 使用多个读卡器加载 PKCS 密钥库时出错
- c - 为什么 task_struct 指针和该结构内的字段之间的差异如此之大?
- sql - 从一列中选择与另一列共享值的值
- npm - 如何正确设置本地 ReasonMl / Bucklescript 依赖项
- typescript - WebStorm 在“??”上给出 TypeScript 错误 操作员
- javascript - LET vs VAR 以及从块作用域提升到全局作用域
- swift - iOS 和 AudioKit - 使用 AKNodeFFTPlot 显示 FFT:如何限制频率范围?